supra
supra

Reputation: 1

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

Upvotes: -1

Views: 53

Answers (1)

Verpous
Verpous

Reputation: 746

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

Upvotes: 0

Related Questions