Sponsored Content
Top Forums Shell Programming and Scripting [BASH] Script to manage background scripts (running, finished, exit code) Post 302921365 by sea on Thursday 16th of October 2014 03:10:39 PM
Old 10-16-2014
Ok, here's the smallest i could get...
But now script3 gets lost among the way, everything else seems to be working fine...

Code:
#!/bin/bash
#
#	Variables
#
	scripts_remains=( "${@}" )
	scripts_total=${#scripts_remains[@]}
	TMP_DIR=$HOME/.cache/$$
	
	# Filled in the process
	unset scripts_todo[@] scripts_id[@]
	unset done_scripts[@] done_ret[@]
	
	# Defaults
	LIMIT=5
	WAIT=5
	
	# Counters - Fixed
	counter_done=0
	
	# Counters - Dynamic
	counter_start=0
	counter_running=0
#
#	Environment check
#
	[[ -d "$TMP_DIR" ]] || mkdir -p "$TMP_DIR"
#
#	Display & Action --> limit 5, passing 4
#
	while [[ $counter_done -lt $scripts_total ]]
	do	# Loop the menu & Reset some values
		
		# Step 1
		# Check if there are files to be started
		echo "Scripts @ start"
# The Limit check is worthless, even when set to 2, all passed scripts gets executed on first loop...
		if [[ $counter_running -lt $LIMIT ]]
		then	# So we look in the script_remains for tasks
			num=0
			for S in ${scripts_remains[@]};do
				[[ -z "$S" ]] && break
				# Generate the command & save to new array
				[[ [./] = "${S:0:1}" ]] && PRE="" || PRE="./" 
				cmd="$PRE$S ; echo \$? > $TMP_DIR/$S.tmp"
				echo "Starting: $S"
				(eval $cmd) &
				scripts_id[$counter_start]="$!"
				scripts_todo[$counter_start]="$S"
				unset scripts_remains[$num]
				((counter_start++))
				((counter_running++))
				((num++))
			done
		fi
		
		# Step 2
		# Print status of already done scripts
		echo "Scrips @ done"
		C=0
		for D in "${done_scripts[@]}";do
			R=${done_id[$C]}
			echo "$D ended $R"
			((C++))
		done
		
		# Step 3
		# Show current tasks running -- now loops here endlessly...  because a script gets lost within the loop
		num=0
		echo "Scripts @ work"
		for W in "${scripts_todo[@]}";do
			# Only display if array element is not empty
			if [[ ! -z "$W" ]]
			then	val=${scripts_id[$num]}
				if [[ ! -z "$val" ]]
				then	if ps -ha | grep $val|grep -v -q grep
					then	echo "$W works : $val"
					else	echo "$W has ended..."
						# Unset this item now
						done_scripts[$counter_done]="$W"
						read R < $TMP_DIR/$W.tmp
						done_id[$counter_done]="$R"
						unset scripts_todo[$num] scripts_id[$num]
						((counter_done++))
						((counter_running--))
					fi
				fi
				((num++))
			fi
			
		done
		
		[[ $counter_done -lt $scripts_total ]] && \
			echo "wait for update: $WAIT" && \
			sleep $WAIT && \
			clear
	done
#
#	Clean up temp files
#
	rm -fr "$TMP_DIR"

BTW: These are my test scripts
Code:
grep -n sleep *
script1:2:sleep 30 
script2:2:sleep 9
script3:2:sleep 20
script4:2:sleep 15


Last edited by sea; 10-16-2014 at 04:29 PM..
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

exit status of Invoking two or more scripts in background

I have a sript which is going to trigger other 3 scripts in background simultaneously for eg: Main Script:(main.sh) ----------- sh a.sh & sh b.sh & sh c.sh & How to catch the exit status and store it in a variable for all those three scripts in main script. Is there any other way of... (4 Replies)
Discussion started by: Omkumar
4 Replies

2. UNIX for Dummies Questions & Answers

background job finished notification

In my last job someone gave me the command to put in my .profile that let me know when a job I had running in the background finished. It was a word about 5 char long. I can't remember it! (4 Replies)
Discussion started by: nkeller
4 Replies

3. Shell Programming and Scripting

Issues with exit after running jobs in background

I have the following sample script to run a script the jobs with the same priority(in this case field3) in parallel; wait for the jobs to finish and run the next set of jobs in parallel.When all the lines are read exit the script. I have the following script which is doing evrything I want... (1 Reply)
Discussion started by: hyennah
1 Replies

4. Shell Programming and Scripting

Capturing the exit status of the script running in background

Hi All, I have a scenario where I am executing some child shell scripts in background (using &)through a master parent script. Is there a way I can capture the exit status of each individual child script after the execution is completed. (2 Replies)
Discussion started by: paragkalra
2 Replies

5. Shell Programming and Scripting

Catch exit code of specific background process

Hi all, i hava a specific backgroud process. I have de PID of this process. At some time, the process finish his job, is there any way to catch the exit code? I use "echo $?" normally for commands. Thanks! (2 Replies)
Discussion started by: Xedrox
2 Replies

6. Shell Programming and Scripting

Bash Question: HowTo Exit Script with User Input While Process is Running Mid-Loop?

Hi, I have written a script that allows me to repetitively play a music file $N times, which is specified through user input. However, if I want to exit the script before it has finished looping $N times, if I use CTRL+c, I have to CTRL+c however many times are left in order to complete the loop.... (9 Replies)
Discussion started by: hilltop_yodeler
9 Replies

7. Shell Programming and Scripting

Running scripts in background

Hi, below is my master script wihch inturn runs 2 scripts in background #master_script.sh ./subscript1.sh & ./subscript2.sh & executed the master_script.sh from unix command prompt $ ./master_script.sh it is executing the subscripts and they are completing fine, however master_script.sh is... (2 Replies)
Discussion started by: JSKOBS
2 Replies

8. Shell Programming and Scripting

Problems running scripts in the background

Hi Could someone offer some help on this problem I've got with running a background process. As part of a script that does a stop/start/status for a piece of software called SAS, the following extract is from part of the start step. My issue is that when the script is run, the control... (0 Replies)
Discussion started by: GavP
0 Replies

9. Shell Programming and Scripting

Terminal running bash/rsync script does not close with exit (MacOS High SIerra)

Hello, I am running a bash script to do an rsync back on a computer running MacOS High Sierra. This is the script I am using, #!/bin/bash # main backup location, trailing slash included backup_loc="/Volumes/Archive_Volume/00_macos_backup/" # generic backup function function backup {... (12 Replies)
Discussion started by: LMHmedchem
12 Replies
Num(3o) 							   OCaml library							   Num(3o)

NAME
Num - Operation on arbitrary-precision numbers. Module Module Num Documentation Module Num : sig end Operation on arbitrary-precision numbers. Numbers (type num ) are arbitrary-precision rational numbers, plus the special elements 1/0 (infinity) and 0/0 (undefined). type num = | Int of int | Big_int of Big_int.big_int | Ratio of Ratio.ratio The type of numbers. === Arithmetic operations === val (+/) : num -> num -> num Same as Num.add_num . val add_num : num -> num -> num Addition val minus_num : num -> num Unary negation. val (-/) : num -> num -> num Same as Num.sub_num . val sub_num : num -> num -> num Subtraction val ( */ ) : num -> num -> num Same as Num.mult_num . val mult_num : num -> num -> num Multiplication val square_num : num -> num Squaring val (//) : num -> num -> num Same as Num.div_num . val div_num : num -> num -> num Division val quo_num : num -> num -> num Euclidean division: quotient. val mod_num : num -> num -> num Euclidean division: remainder. val ( **/ ) : num -> num -> num Same as Num.power_num . val power_num : num -> num -> num Exponentiation val abs_num : num -> num Absolute value. val succ_num : num -> num succ n is n+1 val pred_num : num -> num pred n is n-1 val incr_num : num Pervasives.ref -> unit incr r is r:=!r+1 , where r is a reference to a number. val decr_num : num Pervasives.ref -> unit decr r is r:=!r-1 , where r is a reference to a number. val is_integer_num : num -> bool Test if a number is an integer === The four following functions approximate a number by an integer : === val integer_num : num -> num integer_num n returns the integer closest to n . In case of ties, rounds towards zero. val floor_num : num -> num floor_num n returns the largest integer smaller or equal to n . val round_num : num -> num round_num n returns the integer closest to n . In case of ties, rounds off zero. val ceiling_num : num -> num ceiling_num n returns the smallest integer bigger or equal to n . val sign_num : num -> int Return -1 , 0 or 1 according to the sign of the argument. === Comparisons between numbers === val (=/) : num -> num -> bool val (</) : num -> num -> bool val (>/) : num -> num -> bool val (<=/) : num -> num -> bool val (>=/) : num -> num -> bool val (<>/) : num -> num -> bool val eq_num : num -> num -> bool val lt_num : num -> num -> bool val le_num : num -> num -> bool val gt_num : num -> num -> bool val ge_num : num -> num -> bool val compare_num : num -> num -> int Return -1 , 0 or 1 if the first argument is less than, equal to, or greater than the second argument. val max_num : num -> num -> num Return the greater of the two arguments. val min_num : num -> num -> num Return the smaller of the two arguments. === Coercions with strings === val string_of_num : num -> string Convert a number to a string, using fractional notation. val approx_num_fix : int -> num -> string See Num.approx_num_exp . val approx_num_exp : int -> num -> string Approximate a number by a decimal. The first argument is the required precision. The second argument is the number to approximate. Num.approx_num_fix uses decimal notation; the first argument is the number of digits after the decimal point. approx_num_exp uses scien- tific (exponential) notation; the first argument is the number of digits in the mantissa. val num_of_string : string -> num Convert a string to a number. === Coercions between numerical types === val int_of_num : num -> int val num_of_int : int -> num val nat_of_num : num -> Nat.nat val num_of_nat : Nat.nat -> num val num_of_big_int : Big_int.big_int -> num val big_int_of_num : num -> Big_int.big_int val ratio_of_num : num -> Ratio.ratio val num_of_ratio : Ratio.ratio -> num val float_of_num : num -> float OCamldoc 2012-06-26 Num(3o)
All times are GMT -4. The time now is 07:22 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy