Linux: Keep That Process Running

For whatever reasons a process can crash. The best is always to find out why and fix it. However, sometimes you can have higher priority tasks and you want that process to be running. With systemd this is an easy way to do it. Add the following line to /etc/crontab:

*/5 * * * *	root	if ! /bin/systemctl is-active --quiet service chronyd; then /bin/systemctl start chronyd; fi

Explanation: systemctl is-active –quiet service chronyd will return 0 of the process is running and something else if it isn’t. If all good then exit = 0, check done. If exit != 0 than we need to start the process. More about exit codes here. This crontab will run every 5 minutes and start chronyd if it isn’t running. To help understanding crontab syntax you can use this website: https://crontab.guru

Another option is to use /etc/cron.X, where X can be:

cron.daily
cron.hourly
cron.monthly
cron.weekly

Leave a Reply

Your email address will not be published. Required fields are marked *