Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 04-08-2009, 12:28 AM   #1
Registered: Sep 2005
Posts: 854

Rep: Reputation: 30
Bash script to check if process is running and send email


Can you please help on giving me a script that will check certain process (httpd, xmail) every hour and send email if it is not running?

I already have a tool to send an email automatically but I need a bash script to include in crontab to run every hour.

Old 04-08-2009, 01:04 AM   #2
LQ Newbie
Registered: Oct 2008
Location: Westside, Oahu, Hawaii
Distribution: Slackware64-current
Posts: 11

Rep: Reputation: 3
if ! `pidof program` ;then
mail -s "program bit the dust" root@localhost

replacing program with your programs cli name
Old 04-08-2009, 01:11 AM   #3
Registered: Jan 2007
Location: Alberta Canada
Distribution: Fedora/Redhat/CentOS
Posts: 70

Rep: Reputation: 19
Here's a simple shell you can use:

Scripts starts on next line:

ps -ef | grep -v grep | grep "some command in the process list" \
> dev/null
if [ $? -ne 0 ]; then
'put your email command here.'

# End of Script

The 'ps -ef' will list all processes on the system.
The '| grep -v grep' will filter out our 'ps' process to prevent a false

The '| grep "some command in the process list"' will find, or will not find, the process you are checking.
When selecting a string to look for, keep in mind you have to look for something unique to the program you want find. A generic search for "mail" will match 'sendmail', 'qmail', etc. so try to include some white space, like: ' xmail '. Don't include dates or process numbers, as they will change.

The '\' just means continue the same command on the next line.
We redirect any output to '> /dev/null' so we don't get a email from cron every time the script runs.

The 'if [ $? -ne 0 ]; then' checks if the second grep found what we were looking for. Since we are only concerned when the process is not found, we check for a not equal to 0 return from the grep command.

Replace the 'put your email command here.' with your own email command.
Remember that cron only sets a basic environment, so if your email program is not in one the standard program dirs,
"/usr/local/bin:/usr/bin:/bin", you must use a complete path to the program, ie.: /usr/mydir/bin/myemailprog {options}
You can put more than 1 command inside the if ... fi statement.
Good luck.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Obtain ip address and check for running process via Bash Script? xconspirisist Programming 10 09-12-2008 02:18 PM
Bash script to check for dead process MaffooClock Other *NIX 12 05-18-2007 06:14 AM
Script to check IP address and send to an email kbrajesh Linux - Networking 4 02-08-2007 04:13 AM
Write a script to send an email from bash kpelczar Linux - Software 5 02-09-2005 05:19 PM
[script] check for a running process mikshaw Linux - Software 2 01-13-2004 09:33 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:04 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration