首页 > 解决方案 > 如何在两个一起工作的脚本完成后自动启动脚本?

问题描述

我实际上用一个shell命令运行了两个python脚本,但是当它们完成后如何自动运行第三个?

你知道我可以在同一个 .sh 中添加一个程序来做到这一点吗?

对于我使用的 .sh 文件:

python script1.py && python script2.py

我想在这个序列之后运行一个 script3.py。有人可以帮助我吗?

(显然我在终端上运行脚本

>chmod u+x shell.sh   
>./shell.sh                                   

      

此致。

标签: pythonubuntu

解决方案


这将仅在前面的所有脚本都成功时执行每个后续脚本,也就是说,如果它们都以 status = 0 退出。请记住&&在脚本之间添加:

python script1.py && python script2.py && python script3.py

例子:

执行所有 3 个脚本:

$ python -c 'print(1); exit(0);' && python -c 'print(2); exit(0);' && python -c 'print(3); exit(0);'
1
2
3

仅执行前 2 个脚本:

$ python -c 'print(1); exit(0);' && python -c 'print(2); exit(1);' && python -c 'print(3); exit(0);'
1
2


推荐阅读