services (apache, postgresql,...) monitoring

Shell script to check Services Availability on My Server

I wrote this script because at some moment on my server, services such as apache2 would just go off and the reason up to now, I havent discovered.

The script can be generalized to monitor other services including postgresql, mysql …

  1. The Idea

The original idea was this, when a service transits from one state (on) to another state (off), email me. The difficulty I faced here is that I had to have a variable that could keep track of state transition.

Later, I just decided to check only for services that are unavailable. If unavailable, the script directly restarts my service. The idea was from here.

The most important part is to say that a service is unavailable in shell script language. This is it:

if (( $(ps -ef | grep -v grep | grep $service | wc -l) == 0 ))

This translates to English Language as

  1. get all currently executing processes excluding the grep process itself (ps -ef | grep -v grep)
  2. from this list identify ONLY $service. (grep $service)
  3. if this set returns nothing then our service is not on  (wc -l, count the number of lines outputted. If no line outputted, then service is not on)

Another important part is how to notify the sysadmin. I used the mail utility. For test purposes, I configured my gmail account as a relay host (Mail Transfer Agent, MTA) by following this tutorial.

The mail utility is abit tricky more on it could be found here.

2. The Script

Overall, this is the script:


#Author: Randy on 12/12/2017
#Purpose: check unavailability of services and alert sysadmin
#Prerequisite: run script as root

#get the system’s host name
host=$(hostname -f)

#destination mail

for service in postgresql apache2 mysql

#get all currently executing processes excluding the grep process itself
#from this list identify ONLY $service.
#if this set returns nothing then our service is not on
if (( $(ps -ef | grep -v grep | grep $service | wc -l) == 0 ))
/etc/init.d/$service start
if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 0 ))
#alert system admin
subject=”$service at $host has been started”
echo “$service at $host was not running and has been started!!!” | mail -s “$subject” $email

subject=”$service at $host is not running!!!”
echo “$service at $host is stopped and cannot be started!!!” | mail -s “$subject” $email

the output you get in your mail is similar to this:


To get the script monitor your server every 5minutes  add crontab entry:

*/5 * * * * /path/to/script

I hope that helps and as a sysadmin, be ready, to always monitor your server. If something is unclear in my explanation, don’t hesitate to drop a comment.


say something

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s