Ubuntu 16.04 The program running under screen often crashes and I often have to manually reboot. How to set up auto-restart 3 times a day. ( I mean crontab to shut down it and run it again )
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
HostFission's solution is good, but if you don't want to check if the program is running you can also kill it and start it like this:
Create a bash file with any name on it (like: restart.sh) and type this:
/usr/bin/screen -SX NameOfTheScreen quit;
/usr/bin/screen -S NameOfTheScreen /path/to/startscript;
-S sets the session name -X executes something on the session
Be careful not to use -S -X name, as this will name the session -X. Use - SX name instead.
Then you can set-up crontab to execute this file as many times as you want during the day.
* 3,6,9,12,15,18,21,0 * * * /path/to/restart.sh >> /path/to/logfile
You need to store the PID of the running process somewhere when it launches, and then later if it is still running send a SIGTERM to the process and wait for it to terminate. The below script will perform these actions.
PROGRAM=/bin/someProgram PIDFILE=/var/run/someProgram.pid if [ -f $PIDFILE ]; then PID = $(cat $PIDFILE) if [ -d /proc/$PID ] && [ "$(readlink -f /proc/$PID/exe)" == "$PROGRAM" ]; then echo -n "Shutting down..." kill $PID wait $PID echo "done." else echo "PID not found, program already terminated." fi rm $PIDFILE fi echo -n "Starting..." $PROGRAM & echo $! > $PIDFILE echo "done."
Then just schedule this in cron to run every X hours.