Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

pid monitor script on linux server

Posted on 2014-01-29
8
Medium Priority
?
363 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 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Network Interface Card (NIC) bonding, also known as link aggregation, NIC teaming and trunking, is an important concept to understand and implement in any environment where high availability is of concern. Using this feature, a server administrator …
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…
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…
Suggested Courses

670 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