Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shell Script to monitor a process

Status
Not open for further replies.

baashha

Programmer
Nov 11, 2001
4
MY
Hi,

I need to run a cron job that will check if a process is running. In the shell script that will be invoked, I am thinking of doing a ps -ef | grep "process name" and based on the output, do the processing.

Is this the best way to do it? If it is, I have another doubt. If I do the ps statement, I will get 2 lines - 1 for the process and one more for the grep statement. How can I eliminate the grep component from my output?

Regards,

Baashha
 
To eliminate the grep :-
ps -ef | grep process | grep -v grep

Your script could pipe this through wc -l and check for the result being = 0

If so, then the process isn't running.
One by one, the penguins steal my sanity.
 
Or pass grep a regular expression and eliminate a process try:
ps -ef|grep [p]rocess

this will not show the grep process
 
that is so cool :)

Excellent!

(you would not believe how sick I get of typing 'grep -v grep' -- sad of me, I know) Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
THat is way cool,
Now why does it work.

I have been testing this use of the [] and can see how it will be way useful.

Talking with my co-workers we cannot figure out why it works.

Thankx in advance
 
ps -ef | grep [p]rocess

A list of characters enclosed by [ and ] matches any single character in that list.

[p] matches only the character 'c'
[p]process matches only the word 'process'

The grep command searches for lines containing the word 'process' and doesn't select itself because it doesn't contain it ('[p]rocess is not 'process').

Thanks to spankeee for this tip. Jean Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top