Unix Shell - Monitor Process script

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2004 > Monitor Process script





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Monitor Process script
Neil

2004-10-08, 7:49 am

I need to write a script that runs a command to bring up a number of
processes. I can use the ps -ef command to check if these processes
are running and then the processes are taken down (there is a utility
that will return only these processes). There should be 9 processes
running to be successful. I need to populate a log file with an xml
response code for success or failure:

Failure: echo ^<response code ="-2"/^>

Success: echo ^<response code ="0"/^>

How can i run my ps -ef and do a count on the number of rows returned
and populate the log?
Chris F.A. Johnson

2004-10-08, 7:49 am

On 2004-10-08, Neil wrote:
> I need to write a script that runs a command to bring up a number of
> processes. I can use the ps -ef command to check if these processes
> are running and then the processes are taken down (there is a utility
> that will return only these processes). There should be 9 processes
> running to be successful. I need to populate a log file with an xml
> response code for success or failure:
>
> Failure: echo ^<response code ="-2"/^>
>
> Success: echo ^<response code ="0"/^>
>
> How can i run my ps -ef and do a count on the number of rows returned
> and populate the log?


Pipe the output through wc -l:

num=`COMMAND | wc -l`

--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
Stephane CHAZELAS

2004-10-08, 7:49 am

2004-10-8, 02:14(-07), Neil:
> I need to write a script that runs a command to bring up a number of
> processes. I can use the ps -ef command to check if these processes
> are running and then the processes are taken down (there is a utility
> that will return only these processes). There should be 9 processes
> running to be successful. I need to populate a log file with an xml
> response code for success or failure:
>
> Failure: echo ^<response code ="-2"/^>
>
> Success: echo ^<response code ="0"/^>
>
> How can i run my ps -ef and do a count on the number of rows returned
> and populate the log?


Maybe:

ps -e -o comm= | grep -c 'process-name'

Or, if you know the pids of the processes:

set -- $(ps -o pid= -p "123 124 125...")
if [ "$#" -ne 9 ]; then
echo '^<response code ="-2"/^>'
else
echo '^<response code ="0"/^>'
fi

If you're the owner of those processes, you can do:

if kill -0 12 123 345... 2> /dev/null; then
: all the pids exist
else
: some of them do not exist
fi

Depending on your system, there may be other commands (pidof,
pgrep, ps -C...).

--
Stephane
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com