LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices

Reply
 
Search this Thread
Old 04-07-2009, 11:28 PM   #1
depam
Member
 
Registered: Sep 2005
Posts: 824

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


Hi,

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.

Thanks.
 
Old 04-08-2009, 12:04 AM   #2
laitcg
LQ Newbie
 
Registered: Oct 2008
Posts: 8

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

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

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

Scripts starts on next line:
#!/bin/bash

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

# 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
positive.

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.
 
  


Reply


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 01:18 PM
Bash script to check for dead process MaffooClock Other *NIX 12 05-18-2007 05:14 AM
Script to check IP address and send to an email kbrajesh Linux - Networking 4 02-08-2007 03:13 AM
Write a script to send an email from bash kpelczar Linux - Software 5 02-09-2005 04:19 PM
[script] check for a running process mikshaw Linux - Software 2 01-13-2004 08:33 PM


All times are GMT -5. The time now is 11:45 PM.

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