Linux 在另一个(已经运行)完成后启动脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7485776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Start script after another one (already running) finishes
提问by skd
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?
所以我有一个进程在运行,需要几个小时才能完成。我想在该进程完成后立即自动启动另一个进程。请注意,我不能在第一个脚本中添加对第二个脚本的调用,也不能创建另一个依次运行这两个脚本的调用。有没有办法在 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.
编辑:一种选择是x
使用 pgrep每分钟轮询一次并检查该过程是否完成。如果是这样,请开始另一个。但是,我不喜欢这种解决方案。
PS: Both are bash scripts, if that helps.
PS:两者都是 bash 脚本,如果有帮助的话。
采纳答案by sorpigal
Polling is probably the way to go, but it doesn't have to be horrible.
投票可能是可行的方法,但它不一定是可怕的。
pid=$(ps -opid= -C your_script_name)
while [ -d /proc/$pid ] ; do
sleep 1
done && ./your_other_script
回答by thiton
Given the PID of the first process, the loop
给定第一个进程的PID,循环
while ps -p $PID; do sleep 1; done ; script2
should do the trick. This is a little more stable than pgrep and process names.
应该做的伎俩。这比 pgrep 和进程名称更稳定。
回答by ks1322
You can wait
already running process using bash built-in command wait
. man bash.
您wait
已经可以使用 bash 内置命令运行进程wait
。人 bash。
wait [n ...] Wait for each specified process and return its termination status. Each n may be a process ID or a job specification; if a job spec is given, all processes in that job's pipeline are waited for. If n is not given, all currently active child processes are waited for, and the return status is zero. If n specifies a non-existent process or job, the return status is 127. Otherwise, the return status is the exit status of the last process or job waited for.
wait [n ...] 等待每个指定的进程并返回其终止状态。每个 n 可以是进程 ID 或作业规范;如果给出了作业规范,则等待该作业管道中的所有进程。如果未给出 n,则等待所有当前活动的子进程,返回状态为零。如果 n 指定一个不存在的进程或作业,则返回状态为 127。否则,返回状态为最后等待的进程或作业的退出状态。
回答by shivams
Often it happens that your program is running several demons. In that case your pid will be an array. Just use:
通常情况下,您的程序正在运行多个恶魔。在这种情况下,您的 pid 将是一个数组。只需使用:
PID=($(pidof -x process_name)) #this saves all the PIDs of the given process in the $pid array
PID=($(pidof -x process_name)) #这将给定进程的所有PID保存在$pid数组中
Now, just modify the thiton's code as :
现在,只需将 thiton 的代码修改为:
while ps -p ${PID[*]}; do sleep 1; done ; script2
while ps -p ${PID[*]}; do sleep 1; done ; script2
回答by Yangchuan Li
Maybe you can press ctrl+z first and enter
也许你可以先按 ctrl+z 并输入
fg; echo "first job finished"
回答by Servando Flores
I had a similar problem and solved it this way:
我有一个类似的问题,并以这种方式解决了它:
nohup bash script1.sh &
nohup bash script1.sh &
wait
等待
nohup bash script2.sh &
nohup bash script2.sh &
回答by Cosmtar
The easiest way:
最简单的方法:
./script1.sh && ./script2.sh
The &&says wait for the successful completion of script1 before proceeding to script2.
该&&说等待SCRIPT1顺利完成在进行SCRIPT2之前。
回答by PonnuKumar Ramalingam
I had the same requirement and solved it in the following way:
我有同样的要求并通过以下方式解决了它:
while [[ "$exp" != 0 ]]; do
exp=$(ps -ef |grep -i "SCRIPT_1" |grep -v grep |wc -l)
sleep 5;
done
call SCRIPT_2
调用 SCRIPT_2