Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

pid monitor script on linux server

Posted on 2014-01-29
8
Medium Priority
?
366 Views
Last Modified: 2014-01-31
I am trying to figure out. how to monitor a pid. if the service/pid is down. it should send an email.
0
Comment
Question by:pranav987
  • 4
  • 3
8 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 2000 total points
ID: 39818424
Let's assume the process is called "myservice":

if ! ps -ef | grep -q [m]yservice;  then
    echo "myservice on Host $(hostname) down" | mailx -s "Process Alert" recipient@domain.tld
fi

Open in new window

You could run the script in regular intervals via cron.
0
 
LVL 13

Expert Comment

by:Sandy
ID: 39823438
or you can run the above script once with for loop and sleep interval.
0
 

Author Comment

by:pranav987
ID: 39824673
i tried running the below script.

#!/bin/bash

if ! ps -ef | grep -q 123;  then
    echo "myservice on Host $(hostname) down" | mail -s "Process Alert" xxx@xxx.com
fi


it is not emailing me.
0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39824821
I'm rather sure that the string "123" almost always appears somewhere in the process list, so grep would find it and the script would of course not send mail.

Do you really want to search for the numeric PID? Are you aware that this number will change on each restart of the process (with a few exceptions linke "init" etc.)?

That's why I suggested searching for the command name instead of the PID.

But if it must be the PID and if this PID is indeed always "123" you can try "grep -w" to search for "123" between word boundaries:

if ps -ef | grep -qw 123 ; then
...


wmp
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39824989
Reducing the output of "ps" to show just the PID is certainly a bit more reliable:

#!/bin/bash
if ! ps -eo pid | grep -qw 123;  then
    echo "myservice on Host $(hostname) down" | mail -s "Process Alert" xxx@xxx.com
fi

This works with my initial suggestion (using the command name) as well:

#!/bin/bash
if ! ps -eo comm | grep -qw "myservice" ;  then
    echo "myservice on Host $(hostname) down" | mail -s "Process Alert" xxx@xxx.com
fi
0
 

Author Comment

by:pranav987
ID: 39825569
whenever i grep. its generating below value also and making script not to work.

ex:--
ps -ef |grep 123
root     14582  5590  0 18:13 pts/8    00:00:00 grep 123

can you please suggest. how to avoid this.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39825576
grep -w [1]23

or

grep -v grep | grep -w 123

Did you read my remarks about PIDs above?
0
 

Author Comment

by:pranav987
ID: 39825637
thanks woolmilkporc. i did not use [].
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question