linux check if a service is running
I was working on a Zabbix server that kept failing for some reason. I decided to not get frustrated over it and write a small bash script to restart the service:
#!/bin/bash #List services to check for SERVICES="zabbix_server" #What is the server hostname HOST="My Host" # check if each service is running for service in $SERVICES do # if current service is running, dont do anything. if ps ax | grep -v grep | grep $SERVICES > /dev/null then echo "$service service is running!" else # otherwise, turn it on echo "$SERVICES is not running" sudo /etc/init.d/$SERVICES start if ps ax | grep -v grep | grep $SERVICES > /dev/null then echo "$HOST $SERVICES is now running." echo "$HOST $SERVICEs is now running!" | mail -s "$HOST $SERVICES running" root else echo "$HOST Unable to start $SERVICES." echo "$HOST $SERVICES is not running!" | mail -s "$HOST $SERVICES running" root fi fi done
At least I won't have to be scared of not realizing the unknown while I troubleshoot the issue!
This works perfect! But make sure that the SERVICE name is not a substring in the name of the script which runs this piece of code.
For eg. If the service you want to check is “xyz”, the script name cannot be “verifyxyz”, “checkxyzdaemon”, etc.
Great man,
That was what I’m looking for. Many thanks!