Sponsored Content
Top Forums Shell Programming and Scripting [BASH] Script to manage background scripts (running, finished, exit code) Post 302921589 by sea on Saturday 18th of October 2014 04:15:46 PM
Old 10-18-2014
Thank you guys, i've tried to adapt some of your suggestions, /proc/$pid, kill -0 $pid, kill($pid,0) but none of them worked as expected.
The hint with a "single id number" was great, that way i just managed to make it even smaller than the previous example, with full functionality! Smilie

Screenshot shows how it returns the number of successfully executed scripts, when passed -c...

Below is the 'core' snippet, full script can be seen on https://github.com/sri-arjuna/tui/bl...er/bin/tui-psm
And it is part of: https://github.com/sri-arjuna/tui.git

Hope you like it, and thank you for your help!
Code:
#
#	Variable presets
#
	script_name=( "${@}" )		# Contains all files
	MAX=${#script_name[@]}		# Max amount of scripts
	script_status=( $(for s in "${script_name[@]}";do echo "3";done) )		# Status with same counter of that file	: done(0) failed(1) running(2) todo(3)
	script_pid=()			# PID with the same counter of that file

	# Counters
	RUN=0			# How many scripts are currently running
#
#	Display & Action
#
	while [[ $DONE -lt $MAX ]]
	do	# Loop the menu
		C=0
		DONE=0			# How many scripts are 'done' (regardless of status)
		GOOD=0			# How many scripts ended succesffully
		if ! $QUIET
		then	clear
			tui-header "$TITLE ($script_version)" "$(date +'%F %T')"
			tui-title "Status"
		fi
		
		while [[ $C -lt $MAX ]]
		do	# Vars
			STATUS="${script_status[$C]}"	# Current status
			RET_FILE="$TEMP/$(basename ${script_name[$C]}).ret"
		
			# Do action according to current status
			case $STATUS in
			2)	# IS PID still available?
				pid=${script_pid[$C]} 
				if [[ ! -z "$(echo $pid)" ]]
				then	#if ! ls /proc/${script_pid[$C]}
					if ! ps $pid > /dev/zero
					#if kill -0 $pid  2>&/dev/null
					then	# Its finished
						read RET < "$RET_FILE"
						[[ -z "$RET" ]] && RET=1
						script_status[$C]=$RET
						((RUN--))
					fi
				else 	tui-status 1 "This should not happen, empty pid while running"
				fi
				;;
			3)	# Its TODO, can we start it?
				if [[ $RUN -lt $LIMIT ]] || [[ $LIMIT -eq 0 ]]
				then 	script_status[$C]=2
					STATUS=2
					((RUN++))
					script="${script_name[$C]}"
					[[ [./] = "${script:0:1}" ]] && PRE="" || PRE="./"
					cmd="\"${PRE}$script\" ; echo \$? > \"$RET_FILE\""
					touch "$RET_FILE"
					( eval "$cmd" ) &
					script_pid[$C]=$!
				fi
				;;
			*)	((DONE++))
				[[ $STATUS -eq 0 ]] && ((GOOD++))
				;;
			esac

			# Display latest status
			if ! $QUIET
			then	case $STATUS in
				0|1)	tui-status $STATUS "Finished ${script_name[$C]}"  ;;
				2)	tui-status $STATUS "Running ${script_name[$C]}" "${script_pid[$C]}" ;;
				3)	tui-status $STATUS "Waiting ${script_name[$C]}" ;;
				127)	tui-status 1 "Typo in script: \"${script_name[$C]}\""	;;
				*)	tui-status 1  "Invalid STATUS ($STATUS) on $C ${script_name[$C]}"	;;
				esac
			fi
			((C++))
		done
		
		if ! $QUIET
		then	tui-echo
			tui-title "Summary"
			tui-echo "Scripts completed:" "$DONE/$MAX"
			tui-echo "Currently running:" "$RUN/$LIMIT"
			tui-echo "Successfully executed:" "$GOOD"
			
			tui-echo
			tui-wait $WAIT "Wait for update..."
			echo
		else	sleep $WAIT
		fi
	done

EDIT:
I just think it looks cool, thank you guys!
[BASH] Script to manage background scripts (running, finished, exit code)-tui-bg-scripts-working2jpg
[BASH] Script to manage background scripts (running, finished, exit code)-tui-bg-scripts-progressjpg

Last edited by sea; 01-29-2015 at 02:41 PM..
This User Gave Thanks to sea For This Post:
 

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
All times are GMT -4. The time now is 10:15 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy