在另一个(已经运行)完成后开始脚本

发布于 2024-12-05 07:04:59 字数 230 浏览 1 评论 0原文

所以我有一个进程正在运行,需要几个小时才能完成。我想在一个进程完成后立即自动启动另一个进程。请注意,我无法在第一个脚本中添加对第二个脚本的调用,也无法创建另一个按顺序运行这两个脚本的脚本。在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

z祗昰~ 2024-12-12 07:04:59

给定第一个进程的 PID,循环

while ps -p $PID; do sleep 1; done ; script2

应该可以解决问题。这比 pgrep 和进程名称更稳定一些。

Given the PID of the first process, the loop

while ps -p $PID; do sleep 1; done ; script2

should do the trick. This is a little more stable than pgrep and process names.

只是一片海 2024-12-12 07:04:59

也许您可以首先按CTRL+Z并输入

fg; echo "first job finished"

Maybe you can press ctrl+z first and enter

fg; echo "first job finished"
风筝有风,海豚有海 2024-12-12 07:04:59

投票可能是要走的路,但不必太可怕。

pid=$(ps -opid= -C your_script_name)
while [ -d /proc/$pid ] ; do
    sleep 1
done && ./your_other_script

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
纵性 2024-12-12 07:04:59

您可以使用BASH内置命令等待等待等待。 man bash

等待[n ...]等待每个指定的过程并返回其
终止状态。每个n可以是过程ID或工作规范;
如果给出了工作规格,则该作业管道中的所有过程均为
等待。如果不给出n,所有当前活跃的儿童流程
等待,返回状态为零。如果n指定
不存在的过程或工作,返回状态为127。否则,
返回状态是最后一个过程或工作等待的退出状态
for。

You can wait already running process using bash built-in command wait. man 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.

仅一夜美梦 2024-12-12 07:04:59

通常情况下,您的程序正在运行多个恶魔。在这种情况下,你的 pid 将是一个数组。只需使用:

PID=($(pidof -x process_name)) #这将给定进程的所有 PID 保存在 $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:

PID=($(pidof -x process_name)) #this saves all the PIDs of the given process in the $pid array

Now, just modify the thiton's code as :

while ps -p ${PID[*]}; do sleep 1; done ; script2

猫弦 2024-12-12 07:04:59

watch -g ps -opid -p {目标PID};命令

您可以使用此命令在进程完成后运行特定命令。该进程通过其 PID 来识别。

watch -g ps -opid -p {targetPID}; command

You can use this command to run a specific command after a process finishes. The process is identified via its PID.

南城旧梦 2024-12-12 07:04:59

我有一个类似的问题并通过以下方式解决:

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 &

流殇 2024-12-12 07:04:59

我有相同的要求,并通过以下方式解决了:

while [[ "$exp" != 0 ]]; do
exp=$(ps -ef |grep -i "SCRIPT_1" |grep -v grep |wc -l)
sleep 5;
done

call script_2

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

把梦留给海 2024-12-12 07:04:59

最简单的方法:

./script1.sh && ./script2.sh

&& 表示等待 script1 成功完成,然后再继续执行 script2。

The easiest way:

./script1.sh && ./script2.sh

The && says wait for the successful completion of script1 before proceeding to script2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文