Simple Shell or Bash Script To Check If Service Is Running & Restart It When Dead
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 watch-service.sh
Check For Status and Restart A Service Manually
./watch-service.sh SERVICE_NAME_HERE
Example
./watch-service.sh postgresql
Check apache2 status every minute and restart automatically when it's stopped
please modify the location watch-service.sh
* * * * * /opt/watch-service.sh apache2 > /dev/null
For other services like mysql, postgresql, nginx etc
* * * * * /opt/watch-service.sh nginx > /dev/null * * * * * /opt/watch-service.sh postgresql > /dev/null * * * * * /opt/watch-service.sh 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.
Comments
Leave a comment
You are not LoggedIn but you can comment as an anonymous user which requires manual approval. For better experience please Login.