在另一个(已经运行)完成后开始脚本
所以我有一个进程正在运行,需要几个小时才能完成。我想在一个进程完成后立即自动启动另一个进程。请注意,我无法在第一个脚本中添加对第二个脚本的调用,也无法创建另一个按顺序运行这两个脚本的脚本。在Linux下有什么办法可以做到这一点吗?
编辑:一种选择是使用 pgrep 每 x 分钟轮询一次并检查该过程是否完成。如果是这样,请启动另一项。但是,我不喜欢这个解决方案。
PS:两者都是 bash 脚本,如果有帮助的话。
So I have a process running, and it will take several hours to complete. I would like to start another process right after that one finishes, automatically. Notice that I can't add a call to the second script in the first one, neither create another which sequentially runs both. Is there any way to do this in Linux?
Edit: One option is to poll every x
minutes using pgrep and check if the process finished. If it did, start the other one. However, I don't like this solution.
PS: Both are bash scripts, if that helps.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
给定第一个进程的 PID,循环
应该可以解决问题。这比 pgrep 和进程名称更稳定一些。
Given the PID of the first process, the loop
should do the trick. This is a little more stable than pgrep and process names.
也许您可以首先按CTRL+Z并输入
Maybe you can press ctrl+z first and enter
投票可能是要走的路,但不必太可怕。
Polling is probably the way to go, but it doesn't have to be horrible.
您可以使用BASH内置命令
等待
等待等待。 man bash 。You can
wait
already running process using bash built-in commandwait
. man bash.通常情况下,您的程序正在运行多个恶魔。在这种情况下,你的 pid 将是一个数组。只需使用:
现在,只需将 thiton 的代码修改为:
while ps -p $ {PID[*]};睡觉1;完毕 ;脚本2
Often it happens that your program is running several demons. In that case your pid will be an array. Just use:
Now, just modify the thiton's code as :
while ps -p ${PID[*]}; do sleep 1; done ; script2
您可以使用此命令在进程完成后运行特定命令。该进程通过其 PID 来识别。
You can use this command to run a specific command after a process finishes. The process is identified via its PID.
我有一个类似的问题并通过以下方式解决:
nohup bash script1.sh &
等待
nohup bash script2.sh &
I had a similar problem and solved it this way:
nohup bash script1.sh &
wait
nohup bash script2.sh &
我有相同的要求,并通过以下方式解决了:
call script_2
I had the same requirement and solved it in the following way:
call SCRIPT_2
最简单的方法:
&& 表示等待 script1 成功完成,然后再继续执行 script2。
The easiest way:
The && says wait for the successful completion of script1 before proceeding to script2.