Solved

pid monitor script on linux server

Posted on 2014-01-29
8
353 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Daily system administration tasks often require administrators to connect remote systems. But allowing these remote systems to accept passwords makes these systems vulnerable to the risk of brute-force password guessing attacks. Furthermore there ar…
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.:
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

759 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now