[BASH] Script to manage background scripts (running, finished, exit code)
Heyas,
Since this question (similar) occur every now and then, and given the fact i was thinking about it just recently (1-2 weeks) anyway, i started to write something
Now i want to write a script that will work for/with:
'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
So I had it running and working, but then figured, I've only tested 4 scripts (sleep [5-30secs), but the LIMIT was 5, so I reduced the limit to 2, now i cant figure why its not working.
Actually, it wont even run (properly) with the default LIMIT of 5 even when only 4 scripts are passed
And the previous code no longer works neither, since I've renamed some of the variables, for a better 'sort'.
My approach is:
scripts_remains = array of arguments (the scripts passed)
scripts_start = array is filled only temporary, as long the counter scripts_running is less than the LIMIT.
While filling the scripts_start, i remove the according array element from scripts_remains.
scripts_todo = array that gets filled as soon the scripts_start entry is started. (and afterwards removed from scripts_start)
scripts_id = array which holds the PID of the script of the same ID of scripts_todo
When the script no longer finds the PID, it moves the element from scripts_todo/scripts_id to done_scripts, and fills done_ret, both with the same array id.
(this part works still/again)
But the starting array gets only the first script and not more... however, it does list as many entries as the LIMIT allows, but all contain the first script and only 1 entry does actually show the proper PID.
scripts_remains still holds all array elements, which confuses me, furthermore scripts_todo holds only 1 array element. but lists as many entries as the LIMIT allows.
NOTE: This script is(will be) part of my TUI (text user interface) package, therefore all those tui-* commands ARE available and 'valid' - on my system at least.
Please see the screen-shot on how it outputs, and the code segment that is faulty.
Acknowledge, that the error referring to line 332 is due to the wrong set / missing array values.
I have a small script, that is working, but since i want/need to have some handlers and argument handling around it,
it does in no way refer the issues i experience.
I'm asking if you either see a 'wrong'-code, in the part visible, or if you would have an advice for another approach/method to achieve my goal?
Thank you in advance
EDIT:
It seems that the first script (only one loaded to array) is started/executed as many times as the limit is set to, without continuing to the next array element..
But i cant pinpoint it.
If you need the full code, i'd happily share it, just since it wont run (unless you have TUI installed) i thought its not really helpfull anyway.
EDIT:
Done the 'no limit' part, which works:
So I need to get this code segment working with a LIMIT factor, so only LIMIT amount of scripts are started as long scripts_running is smaller than the LIMIT - see the main post -
Screen-shot *1*small : the non-working part
Screen-shot *2*small : the working no-limit part
EDIT3:
I'm now 100% sure, I've had somewhere screwed up with the arrays, but atm I'm too blind...
If there's more than one thing in that array, this will die with 'too many arguments' or 'unexpected argument' or the like for cramming [[ ! -z "a" "b" "c" "d" "..." ]] into your statement.
Try while [[ ! -z "${scripts_remains[*]}" ]]
I might be able to comment more on your code if you posted anything except screenshots.
If it's too big to post, trim it down! Make a minimal example that still shows the problem. There's probably good odds that doing so will actually find the problem, too.
I'd be tempted to just use strings and/or positional parameters instead of arrays here, too. They're much easier to deal with in a lot of circumstances.
Last edited by Corona688; 10-16-2014 at 01:24 PM..
Refinition of the problem:
It is now starting script1 - three times at once, while removing (but only once) script2, but then never again any other script from the remains list.
As well as adding another tripple bunch script1's as soon the previous three script1 ones finsihed, appending them infitie to done list..
Right, minimal example... working on...
EDIT:
In the screenshot *4_small* you see again, the '4' within an error messsage - but labled as /script1 - both a path&name issue, but in the screenshot *3_small* the 4th script is not listed at all.
I don't think "unset" really does what you think it does -- deleting from the middle of an array like that. Whenever I do that I end up with "holes", indexes that still exist but have no value. Associative arrays in shell is pretty chancy anyhow, you often won't have a bash new enough, or have bash at all.
Why keep an array of arguments when you already have one, $@
A construct I often use:
Last edited by Corona688; 10-16-2014 at 05:54 PM..
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)
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)
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)
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)
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)
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)
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)
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)
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)