运行多条python的linux bash脚本。

时间:2020-12-07 15:42:53

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

我有两个python脚本a。py和b。py和我想要编写一个bash脚本,它将加载a。不运行b。py之前。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是做…我该怎么做呢?

2 个解决方案

#1


23  

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。而且只运行python b。py如果python。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 

#2


1  

prompt_err() {

echo -e "\E[31m[ERROR]\E[m"

echo - E " \ E[31 m(错误)\ E[m”

}

}

prompt_ok() {

prompt_ok(){

echo -e "\E[32m[OK]\E[m"

echo - E " \ E[32 m[好]\ E[m”

}

}

status() {

状态(){

if [ $1 -eq 0 ]; then

如果[$1 -eq 0];然后

prompt_ok

prompt_ok

else prompt_err

其他prompt_err

exit -1

出口1

fi

fi

}

}

a.py

a.py

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'.

如果一个。py才会处理b。py',否则它将以'Error'退出。

#1


23  

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。而且只运行python b。py如果python。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 

#2


1  

prompt_err() {

echo -e "\E[31m[ERROR]\E[m"

echo - E " \ E[31 m(错误)\ E[m”

}

}

prompt_ok() {

prompt_ok(){

echo -e "\E[32m[OK]\E[m"

echo - E " \ E[32 m[好]\ E[m”

}

}

status() {

状态(){

if [ $1 -eq 0 ]; then

如果[$1 -eq 0];然后

prompt_ok

prompt_ok

else prompt_err

其他prompt_err

exit -1

出口1

fi

fi

}

}

a.py

a.py

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'.

如果一个。py才会处理b。py',否则它将以'Error'退出。