Sponsored Content
Top Forums Shell Programming and Scripting [BASH] Script to manage background scripts (running, finished, exit code) Post 302921483 by drl on Friday 17th of October 2014 10:35:16 AM
Old 10-17-2014
Hi.

Obviously a lot of work and thought has been put into this. As far as I know, the requirements:
Code:
    * 'any' amount of scripts passed,
    * provides a limiter, so only X scripts are running simultaneously
    * Prints the status code of each of the scripts ran

can be accomplished through utilities xargs and GNU parallel. Here's an example of a number of script names in a data file that will be "executed". First singly (not in parallel), then at most n=2 at a time. The script that "runs" the script name is run with ksh so that the number of processes can be more easily seen.
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate parallel execution of scripts, GNU parallel.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C parallel

FILE=${1-data1}

pl " Input data file, names of scripts, $FILE:"
cat $FILE

pl " Master execution script s0:"
cat s0

pl " Results:"
parallel ./s0 < $FILE

pl " Results, limitation of 2 scripts simultaneously:"
parallel --jobs 2 ./s0 < $FILE

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian 5.0.8 (lenny, workstation) 
bash GNU bash 3.2.39
parallel GNU parallel 20111122

-----
 Input data file, names of scripts, data1:
foo
bar
baz
qux
quux
corge

-----
 Master execution script s0:
#!/usr/bin/env ksh

# @(#) s0	Demonstrate script to execute a script from argument 1.

printf " $0: would have executed script %s here, process $$\n" $1
printf " exit status of script %s is %d\n" $1 $?
printf " Current processes:\n"
ps
sleep 1

exit 0

-----
 Results:
 ./s0: would have executed script foo here, process 12774
 exit status of script foo is 0
 Current processes:
  PID TTY          TIME CMD
 3451 pts/13   00:00:01 bash
12670 pts/13   00:00:00 bash
12734 pts/13   00:00:00 parallel
12774 pts/13   00:00:00 ksh
12775 pts/13   00:00:00 ps
 ./s0: would have executed script bar here, process 12776
 exit status of script bar is 0
 Current processes:
  PID TTY          TIME CMD
 3451 pts/13   00:00:01 bash
12670 pts/13   00:00:00 bash
12734 pts/13   00:00:00 parallel
12776 pts/13   00:00:00 ksh
12777 pts/13   00:00:00 ps
 ./s0: would have executed script baz here, process 12781
 exit status of script baz is 0
 Current processes:
  PID TTY          TIME CMD
 3451 pts/13   00:00:01 bash
12670 pts/13   00:00:00 bash
12734 pts/13   00:00:00 parallel
12781 pts/13   00:00:00 ksh
12782 pts/13   00:00:00 ps
 ./s0: would have executed script qux here, process 12783
 exit status of script qux is 0
 Current processes:
  PID TTY          TIME CMD
 3451 pts/13   00:00:01 bash
12670 pts/13   00:00:00 bash
12734 pts/13   00:00:00 parallel
12783 pts/13   00:00:00 ksh
12784 pts/13   00:00:00 ps
 ./s0: would have executed script quux here, process 12785
 exit status of script quux is 0
 Current processes:
  PID TTY          TIME CMD
 3451 pts/13   00:00:01 bash
12670 pts/13   00:00:00 bash
12734 pts/13   00:00:00 parallel
12785 pts/13   00:00:00 ksh
12786 pts/13   00:00:00 ps
 ./s0: would have executed script corge here, process 12787
 exit status of script corge is 0
 Current processes:
  PID TTY          TIME CMD
 3451 pts/13   00:00:01 bash
12670 pts/13   00:00:00 bash
12734 pts/13   00:00:00 parallel
12787 pts/13   00:00:00 ksh
12788 pts/13   00:00:00 ps

-----
 Results, limitation of 2 scripts simultaneously:
 ./s0: would have executed script foo here, process 12815
 exit status of script foo is 0
 Current processes:
  PID TTY          TIME CMD
 3451 pts/13   00:00:01 bash
12670 pts/13   00:00:00 bash
12789 pts/13   00:00:00 parallel
12815 pts/13   00:00:00 ksh
12816 pts/13   00:00:00 ps
12817 pts/13   00:00:00 ksh
12818 pts/13   00:00:00 ps
 ./s0: would have executed script bar here, process 12817
 exit status of script bar is 0
 Current processes:
  PID TTY          TIME CMD
 3451 pts/13   00:00:01 bash
12670 pts/13   00:00:00 bash
12789 pts/13   00:00:00 parallel
12815 pts/13   00:00:00 ksh
12817 pts/13   00:00:00 ksh
12818 pts/13   00:00:00 ps
 ./s0: would have executed script baz here, process 12822
 exit status of script baz is 0
 Current processes:
  PID TTY          TIME CMD
 3451 pts/13   00:00:01 bash
12670 pts/13   00:00:00 bash
12789 pts/13   00:00:00 parallel
12822 pts/13   00:00:00 ksh
12823 pts/13   00:00:00 ps
12824 pts/13   00:00:00 ksh
12825 pts/13   00:00:00 ps
 ./s0: would have executed script qux here, process 12824
 exit status of script qux is 0
 Current processes:
  PID TTY          TIME CMD
 3451 pts/13   00:00:01 bash
12670 pts/13   00:00:00 bash
12789 pts/13   00:00:00 parallel
12822 pts/13   00:00:00 ksh
12824 pts/13   00:00:00 ksh
12825 pts/13   00:00:00 ps
 ./s0: would have executed script corge here, process 12828
 exit status of script corge is 0
 Current processes:
  PID TTY          TIME CMD
 3451 pts/13   00:00:01 bash
12670 pts/13   00:00:00 bash
12789 pts/13   00:00:00 parallel
12826 pts/13   00:00:00 ksh
12827 pts/13   00:00:00 ps
12828 pts/13   00:00:00 ksh
12829 pts/13   00:00:00 ps
 ./s0: would have executed script quux here, process 12826
 exit status of script quux is 0
 Current processes:
  PID TTY          TIME CMD
 3451 pts/13   00:00:01 bash
12670 pts/13   00:00:00 bash
12789 pts/13   00:00:00 parallel
12826 pts/13   00:00:00 ksh
12827 pts/13   00:00:00 ps
12828 pts/13   00:00:00 ksh
12829 pts/13   00:00:00 ps

My apologies if I missed the point ... cheers, drl
This User Gave Thanks to drl 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 07:31 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy