Solved

pid monitor script on linux server

Posted on 2014-01-29
8
361 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
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

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

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

I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

617 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