1

seeking everyone's help on my workplace python processes

Context: -I have some python scripts running 24/7 in Powershell to process files that it detects in my folder), and then store them in the database (MySQL). -However sometimes the script would hang (eg. due to a new error encountered, work server crash, etc). -It caused some downtime as only my maintenance team can reset the script and they are not always around.

Trying to do: -Whenever it detects that the script hangs, it will auto restart the scripts.

Apologies that I do not have sample scripts as all of them are stored in my workplace. I would appreciate it if you can share with me the rough thought process or examples (on other sites) so I can 'take off' on my own. thank you :)

panzers1
  • 23
  • 5
  • 1
    What you can do is have a Watchdog script that if doesn't receive a message from the main script every X seconds kills and restarts it. You can check how to send messages between scripts here https://stackoverflow.com/questions/28813210/send-message-to-a-python-script And how to kill process here https://stackoverflow.com/questions/6278847/is-it-possible-to-kill-a-process-on-windows-from-within-python With this you can already start building the Watchdog. –  Jun 15 '22 at 15:18
  • Hi Sembei Norimaki, thanks for your input! i will go and experiment it – panzers1 Jun 16 '22 at 13:32

1 Answers1

0

The following bash script might help you:

#!/bin/bash

scripts=('python3 script1.py' 'python3 script2.py' 'python3 script3.py')

# loop through each python script you want and execute
for s in "${scripts[@]}"; do
    eval $s # run the script

    # pick up the exit code in variabl
    python_exit_status=$? 
    if [ "${python_exit_status}" -ne 0 ];
    then
        echo "Exit code not equal 0. Python script errored ... "
        # here, you want to reset from the beginning. Break and rerun entire bash script?
    else
        echo "Exit code 0. Continue as normal ... "
        # here, do nothing and allow it to continue.
    fi
done

The concept is that you loop through each of your python scripts, pick up on the exit code of said script, and then have a region where the bash code goes when the exit code is not equal to zero (i.e. Python script error/hangup occurs). Not sure what you want to do there though so I did not add any more. Did you want to restart from script1 again if script2 hangs up?