I would like a bash script to execute a command (which reports status information) when a certain job finishes. I am running (with a script) a number of programs and send them to background using nohup. When they finish I would like to execute a script which collects information and reports it. Note that I am aware that I can have a for loop with sleep which checks actively if the pid is still in the jobs list. But I am wondering whether there is a way to tell the system to run this script e.g. by adding options to nohup.
Asked
Active
Viewed 596 times
0
-
3Why not just put the command *inside* the script you launch with nohup? – bmargulies May 17 '12 at 21:58
-
Well, I am running programs (in a different language), but I could in principle write a wrapper script for each which would however be active during the complete execution time of the program itself. – highsciguy May 17 '12 at 22:00
-
1Also take a look at the `wait` command. – Adam Liss May 17 '12 at 22:01
-
You may find also [this post useful to read](http://stackoverflow.com/questions/1570262/shell-get-exit-code-of-background-process) – Brad May 17 '12 at 22:05
3 Answers
1
You could nohup
a script that runs the command that you want to run and then runs the post-completion job afterward.
For example, create runtask.sh
:
runjob
collect_and_report
and then:
% nohup runtask.sh

sblom
- 26,911
- 4
- 71
- 95
0
Start your jobs in a function, and the function in background.
task1plus () {
task1
whenFinished1
}
task2plus () {
task2
whenFinished2
}
task3plus () {
task3
whenFinished3
}
task1plus &
task2plus &
task3plus &

user unknown
- 35,537
- 11
- 75
- 121
0
This might work for you:
{script; report} &
disown -h
or, to make it conditional:
{script && report} &
disown -h
You could even do:
{script && report || fail} &
disown -h

Dennis Williamson
- 346,391
- 90
- 374
- 439