Re: Checking for a script crash, kill crashed script and restart again in background
On boot, crontab starts a wrapper bash script in the background. This wrapper script periodically checks wether an "I am alive" file from another script exists, and if it doesn't, that other script is killed and restarted in the background by the wrapper. If the "I am alive" file does exist, the wrapper deletes it. The other script creates it again upon completion of the next cycle of an infinite loop.
A little log gets updated.
Below I have added a few "\" to ease the reading of the script.
My wrapper BASH script is as follows:
Code: Select all
while true ; do if [ ! -f "/home/kodi-fileserver/watchkodi.alive" ] ; then while /usr/bin/pkill -9 -f "/home/kodi-fileserver/bin/watchkodi" ; \ do /usr/bin/sleep 1 >/dev/null 2>&1 ; \ done (/home/kodi-fileserver/bin/watchkodi &) >/dev/null 2>&1 && \ /usr/bin/echo "$(date +%Y%m%d-%H:%M:%S) restarted" >> /home/kodi-fileserver/log_watchkodi 2>/dev/null \ || /usr/bin/echo "$(date +%Y%m%d-%H:%M:%S) restart failed" >> /home/kodi-fileserver/log_watchkodi 2>/dev/null else /usr/bin/rm -f "/home/kodi-fileserver/watchkodi.alive" >/dev/null 2>&1 fi /usr/bin/sleep 15 >/dev/null 2>&1 done
Is this the correct way of doing things?
I am particularly intersted in whether the while loop with the pkill is correct or not - it does seem to work, but not always and it's hard to nail down how/when it goes wrong.
Thanks for your time and help.