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
SCRIPTMGR(1)															      SCRIPTMGR(1)

NAME
scriptmgr - utility for controlling other skytools scripts. SYNOPSIS
scriptmgr.py [switches] config.ini <command> [-a | job_name ... ] DESCRIPTION
scriptmgr is used to manage several scripts together. It discovers potential jobs based on config file glob expression. From config file it gets both job_name and service type (that is the main section name eg [cube_dispatcher]). For each service type there is subsection in the config how to handle it. Unknown services are ignored. COMMANDS
status scriptmgr config.ini status Show status for all known jobs. start scriptmgr config.ini start -a scriptmgr config.ini start job_name1 job_name2 ... launch script(s) that are not running. stop scriptmgr config.ini stop -a scriptmgr config.ini stop job_name1 job_name2 ... stop script(s) that are running. restart scriptmgr config.ini restart -a scriptmgr config.ini restart job_name1 job_name2 ... restart scripts. reload scriptmgr config.ini reload -a scriptmgr config.ini reload job_name1 job_name2 ... Send SIGHUP to scripts that are running. CONFIG
Common configuration parameters job_name Name for particulat job the script does. Script will log under this name to logdb/logserver. The name is also used as default for PgQ consumer name. It should be unique. pidfile Location for pid file. If not given, script is disallowed to daemonize. logfile Location for log file. loop_delay If continuisly running process, how long to sleep after each work loop, in seconds. Default: 1. connection_lifetime Close and reconnect older database connections. log_count Number of log files to keep. Default: 3 log_size Max size for one log file. File is rotated if max size is reached. Default: 10485760 (10M) use_skylog If set, search for [./skylog.ini, ~/.skylog.ini, /etc/skylog.ini]. If found then the file is used as config file for Pythons logging module. It allows setting up fully customizable logging setup. scriptmgr parameters config_list List of glob patters for finding config files. Example: config_list = ~/dbscripts/conf/*.ini, ~/random/conf/*.ini Service section parameters cwd Working directory for script. args Arguments to give to script, in addition to -d. script Path to script. Unless script is in PATH, full path should be given. disabled If this service should be ignored. Example config file [scriptmgr] job_name = scriptmgr_livesrv logfile = ~/log/%(job_name)s.log pidfile = ~/pid/%(job_name)s.pid config_list = ~/scripts/conf/*.ini # defaults for all service sections [DEFAULT] cwd = ~/scripts [table_dispatcher] script = table_dispatcher.py args = -v [cube_dispatcher] script = python2.4 cube_dispatcher.py disabled = 1 [pgqadm] script = ~/scripts/pgqadm.py args = ticker COMMAND LINE SWITCHES
Following switches are common to all skytools.DBScript-based Python programs. -h, --help show help message and exit -q, --quiet make program silent -v, --verbose make program more verbose -d, --daemon make program go background Following switches are used to control already running process. The pidfile is read from config then signal is sent to process id specified there. -r, --reload reload config (send SIGHUP) -s, --stop stop program safely (send SIGINT) -k, --kill kill program immidiately (send SIGTERM) Options specific to scriptmgr: -a, --all Operate on all non-disabled scripts. 03/13/2012 SCRIPTMGR(1)
All times are GMT -4. The time now is 03:22 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy