Search code examples
linuxsh

Run multiple python scripts with infinite loops parallely in an SH file


I've two Python scripts having infinite while loops inside them. I'd like to trigger both of'em parallelly and let it run in the background using an SH script file. If I run the python scripts separately via SH script file they are getting executed. However, if I try to run both at the same time, the script file is not getting executed. Any help would be much appreciated.

This is my SH file configuration.

#!/bin/sh
source /data/test_venv/bin/activate
/data/test_venv/bin/python3 /data/CV_MQTT.py &
/data/test_venv/bin/python3 /data/CV_Flask.py &

Though the syntax is correct, I'm not sure what I'm missing in the above script.

Note - The access and permissions are available for this sh file.


Solution

  • Using subshells might help in this case. I have not executed below code myself; however, you can give it a try and see if you get what you expect.

    #!/bin/sh
    source /data/test_venv/bin/activate
    (/data/test_venv/bin/python3 /data/CV_MQTT.py) &
    (/data/test_venv/bin/python3 /data/CV_Flask.py) &
    wait  # To wait until processes running inside subshells are complete
    

    It is always good to use 'wait' with parallel subshells; however, in your case it might not make sense as the python scripts have infinite loops and are not going to complete :)

    Regards, Yuvraj