Linux / Unix Script: Simple Process Checker To Find Out If A Service Is Running or Not

A simple shell script to find out whether critical services are running or, not under Linux or Unix operating systems. The script can send notification using email.

#!/bin/bash
# Name : service.chk 
# URL: https://bash.cyberciti.biz/monitoring/simple-process-checker-script/
# Purpose: A simple process checker. Find out if service is running or not.
# Tested on: Debian and RHEL based system only.
# ----------------------------------------------------------------------------
# Author: nixCraft http://www.cyberciti.biz/
# Copyright: 2009 nixCraft under GNU GPL v2.0+
# ----------------------------------------------------------------------------
# Last updated: 13/Mar/2013 - Added support for email and other enhancements 
# Last updated: 05/Dec/2011 - Added support for binary path check
# ----------------------------------------------------------------------------
 
## Change as per your distro 
_pgrep="/usr/bin/pgrep"
_mail="/usr/bin/mail"
 
## Add binary list here
_chklist="/usr/bin/php-cgi /usr/sbin/nginx /usr/sbin/lighttpd /usr/sbin/mysqld /usr/sbin/apache2 /usr/sbin/named /usr/sbin/pgsqld"
 
## yes | no
_sendemail="no"
 
## Add your email id
_email="your@mobile.email.id.example.com"
 
## Do not change below
_failed="false"
_service="Service:"
 
_running() {
	local p="${1##*/}"
	local s="true"
	$_pgrep "${p}" >/dev/null || { s="false"; _failed="true"; _service="${_service} $1,"; }
	[[ "$s" == "true" ]] && echo "$1 running" || { echo -n "$1 not running"; [[ ! -f "$1" ]] && echo " [ $1 not found ]" || echo ; }
}
 
## header
echo "Service status on ${HOSTNAME} @ $(date)"
echo "------------------------------------------------------"
 
## Check if your service is running or not 
for s in $_chklist
do
	_running "$s"
done
 
## Send a quick email update (good for cron jobs) ##
[[ "$_failed" == "true" && "$_sendemail" == "yes" ]] && { _mess="$_service failed on $HOSTNAME @ $(date)"; 
                                 		     $_mail -s 'Service not found' "$_email" < "${_mess}";
                                                   }

Sample outputs:

Fig.01: Script in action (click to enlarge)

Fig.01: Script in action (click to enlarge)

Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics:
CategoryList of Unix and Linux commands
File Management cat
Firewall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN CentOS 8 Debian 10 Firewall Ubuntu 20.04
5 comments… add one
  • Edwin Sineath Jun 22, 2014 @ 12:28

    worked perfectly…once I changed permissions

  • Matt Hall Feb 13, 2014 @ 17:01

    Is there a way to get this to process the “_chklist” with a comma delimited instead of a space between the processes?

  • dima Oct 7, 2013 @ 18:18

    e a Bash shell script that creates 10 files under the directory /tmp/ with the names:

    Foo1, foo2, … etc. then write in each of them “Hello in 1”, Hello in 2”, …etc.

    Then rename them to the names CPE471_0_file, CPE471_1_file , CPE471_9_file , etc.

  • Debian May 21, 2013 @ 13:17

    Won’t send me a message via email. says error on line 51..

    • Sayajin Feb 10, 2014 @ 15:22

      change the section under: ## Send a quick email update (good for cron jobs) ##

      to

      ## Send a quick email update (good for cron jobs) ##
      [[ “$_failed” == “true” && “$_sendemail” == “yes” ]] &” | $_mail -s ‘Service not Running’ “$_email”;
      }

Leave a Reply

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

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.