Script to monitor process on Linux

Environment: Oracle Linux 6.2

Sometimes you want to monitor a process on your Linux server and do some alerting when the process is not running anymore.

The following shell script can be used as an example. It will monitor the “dbconsole” process (database control) and send a e-mail message when the process is not running anymore.

$ vi check_db_control.sh

#!/bin/sh

PROCESSFILE='dbconsole'

if [ `ps ax|grep -v grep|grep -ic $PROCESSFILE` -gt 0 ]
then
        echo "$PROCESSFILE is running, everything is fine"
else
        echo "$PROCESSFILE is not running on `hostname -s`!" | mail -s "$PROCESSFILE down!" matthiash@mydomain.com
fi

exit

$ chmod u+x check_db_control.sh

To schedule the script, you can add it to cron. For example, this script will run every day at 10:00AM:

$ crontab -e

# Monitoring dbconsole process
00 10 * * * /home/oracle/scripts/check_db_control.sh 1>/dev/null 2>&1

 

HTH,
Matthias

One Response to Script to monitor process on Linux

  1. matthiashoys says:

    Update: there was an error in the “if …” line (line 5). I corrected it now!

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: