Solved

pid monitor script on linux server

Posted on 2014-01-29
8
356 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 500 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

820 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