bashdo-whilerestartshebang

Quitting and restarting


I have this bash script that restarts/monitors myscript

while { myscript; rc=$?; true; }; do
    echo "'myscript' crashed with exit code $rc. Restarting..." >&2
    sleep 1
done

basically myscript is a python script that's supposed to continuously run no matter what but sometimes the script will be running but its not working. When this happens the script will print "closed connection" but doesn't restart it will just still be running but the script is not doing anything

When that gets printed I want to quit myscript and restart it. How do I add this logic. When "closed connection" gets printed I want to quit myscript and start it again otherwise continue to run and restart for any other errors


Solution

  • Something like this maybe?

    while true; do
        myscript | while IFS='' read -r line; do
            [[ "$line" == "closed connection" ]] && pkill myscript
        done
    
        echo "'myscript' crashed. Restarting..." >&2
        sleep 1
    done