Very often we are in need to check if a service up and running periodically in linux or ubuntu server and restart them if they are dead. Here is a simple bash script that you can use to check any service like apache, nginx, mysql, postgresql and restart them if they goes down.

Allow permission to execute

chmod +x

Check For Status and Restart A Service Manually



./ postgresql

Check apache2 status every minute and restart automatically when it's stopped

please modify the location

* * * * * /opt/ apache2 > /dev/null

For other services like mysql, postgresql, nginx etc

* * * * * /opt/ nginx > /dev/null
* * * * * /opt/ postgresql > /dev/null
* * * * * /opt/ mysql > /dev/null

Note: This is tested on ubuntu 16.04, ubuntu 18.04, ubuntu 20.04

I would be happy if it's also supports on your server as well.