在另一个终止后运行python脚本 [英] Run python script after another is terminated

查看:171
本文介绍了在另一个终止后运行python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个python脚本,需要几个小时才能完成。我通常将其运行为:

I have a python script that takes several hours to complete. I usually run it as:

python myscript.py arg1 arg2 ... argN 2> log.err > log.out &

我想要一个shell脚本运行几次相同的python脚本(带有不同的参数) )。此脚本必须在第一个脚本终止时启动第二个python脚本,依此类推。我想我应该使用进程的PID并在一些while循环中迭代检查使用sleep。有什么建议?脚本在远程机器中执行,因此我必须将它们放在后台,以便即使我退出也能保证它们继续执行。

I'd like to have a shell script that runs several time the same python script (with different parameters). This script has to start the second python script ONLY when the first one has terminated and so on. I think I should use PID of the processes and iteratively check using sleep within some while loops. Any suggestions? The scripts are executed in a remote machine so I have to put them in background in order the guarantee they keep executing even when I log out.

推荐答案

使用一个脚本逐个运行所有作业而不是背景化python的背景。例如:

Instead of backgrounding each invocation of python, use a script which runs all the jobs one by one and background the script. For example:

#!/bin/sh

python myscript.py arg1 arg2 ... argN 2> log.err > log.out
python myscript.py different_args
# etc.

制作脚本可执行文件( chmod + x script.sh )并从shell运行它,如 ./ script.sh&

Make the script executable (chmod +x script.sh) and run it from the shell like ./script.sh &

也许你应该看看像这样的工具屏幕 ,这将在您注销后保持您的工作正常运行。如果你执行 screen ./script.sh ,然后分离( ctrl-ad )并注销,你的脚本将会继续跑。然后您可以稍后重新登录并使用屏幕-r 重新附加。

Perhaps you should take a look at a tool like screen, which will keep your jobs running after you log off. If you do screen ./script.sh, then detach (ctrl-a-d) and log off, your script will continue to run. Then you can log back in later and reattach with screen -r.

这篇关于在另一个终止后运行python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆