简体   繁体   中英

Execute command when job finishes

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 eg by adding options to nohup.

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

Start your jobs in a function, and the function in background.

task1plus () {
  task1 
  whenFinished1  
}

task2plus () {
  task2
  whenFinished2
}

task3plus () {
  task3
  whenFinished3
}

task1plus &
task2plus &
task3plus &

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM