简体   繁体   English

作业完成时执行命令

[英]Execute command when job finishes

I would like a bash script to execute a command (which reports status information) when a certain job finishes. 我希望bash脚本在特定作业完成时执行命令(报告状态信息)。 I am running (with a script) a number of programs and send them to background using nohup. 我正在运行(使用脚本)许多程序,并使用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. 请注意,我知道我可以有一个带睡眠的for循环,该循环会主动检查pid是否仍在作业列表中。 But I am wondering whether there is a way to tell the system to run this script eg by adding options to nohup. 但是我想知道是否有办法告诉系统运行此脚本,例如通过向nohup添加选项。

You could nohup a script that runs the command that you want to run and then runs the post-completion job afterward. 你可以nohup运行要运行该命令,然后运行后完成作业后的脚本。

For example, create runtask.sh : 例如,创建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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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