运行多个python的linux bash脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13692519/
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
linux bash script running multiple python
提问by StudentOfScience
I have 2 python scripts a.py and b.py and I want to write a bash script that will load a.py and not run b.py until a.py is done doing it's thing. simplistically
我有 2 个 python 脚本 a.py 和 b.py,我想编写一个 bash 脚本,该脚本将加载 a.py 并且在 a.py 完成它之前不运行 b.py。简单地
#!/usr/bin/env bash
python a.py
python b.py
but this is naive, a check to see if a.py is done... how do I do that?
但这太天真了,检查 a.py 是否已完成……我该怎么做?
采纳答案by sampson-chen
This by default will already run one after the other.
默认情况下,这将一个接一个地运行。
To check that python a.py
completed successfully as a required condition for running python b.py
, you can do:
要检查是否python a.py
已成功完成作为运行的必需条件python b.py
,您可以执行以下操作:
#!/usr/bin/env bash
python a.py && python b.py
Conversely, attempt to run python a.py
, and ONLY run 'python b.py' if python a.py
did not terminate successfully:
相反,尝试运行python a.py
,如果python a.py
没有成功终止,则仅运行“python b.py” :
#!/usr/bin/env bash
python a.py || python b.py
To run them at the same time as background processes:
要与后台进程同时运行它们:
#!/usr/bin/env bash
python a.py &
python b.py &
(Responding to comment) - You can chain this for several commands in a row, for example:
(回应评论) - 您可以将其链接为一行中的几个命令,例如:
python a.py && python b.py && python c.py && python d.py
回答by Mansab Uppal
prompt_err() {
echo -e "\E[31m[ERROR]\E[m"
echo -e "\E[31m[ERROR]\E[m"
}
}
prompt_ok() {
prompt_ok() {
echo -e "\E[32m[OK]\E[m"
echo -e "\E[32m[OK]\E[m"
}
}
status() {
status() {
if [ $1 -eq 0 ]; then
if [ $1 -eq 0 ]; then
prompt_ok
prompt_ok
else
prompt_err
else
prompt_err
exit -1
exit -1
fi
fi
}
}
a.py
a.py
status
status
b.py
b.py
You can use the check code above.
您可以使用上面的校验码。
If 'a.py' is done only then it will process 'b.py', otherwise it will exit with an 'Error'.
如果 'a.py' 仅完成,则它将处理 'b.py',否则它将以“错误”退出。