简体   繁体   中英

linux bash script running multiple python

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

#!/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?

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:

#!/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:

#!/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 
prompt_err() {

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

}

prompt_ok() {

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

}

status() {

if [ $1 -eq 0 ]; then

prompt_ok

else prompt_err

exit -1

fi

}

a.py

status

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM