connollyc4 connollyc4 - 3 months ago 8
Bash Question

How to monitor a specific list of process names in a shell or perl script

I am trying to figure out the best way to monitor a list of process id's within a shell script. I don't want to monitor them all, just a custom list.

For example:
If I have process names::

Orange-TE--02
Orange-TE--03
Apple-JI--01
Orange-TE--09
Orange-JI--06


I am trying to write a script that will only Monitor Orange-TE--02, Apple-JI--01, Orange-JI--06 so if any of them goes down an email alert would be sent with the name of which process went down. if Orange-TE--03, Orange-TE--09 goes down, nothing happens.

Here is the output of the process'
process.sh -s

name Process Pid
---------------- ------------------------------- ----
tneal01 Orange-JI--06 3443
tneal01 Orange-TE--09 2233
tneal01 Orange-JI--01 5533
tneal01 Apple-TE--03 9384
tneal01 Orange-TE--02 4992


I was thinking about outputting the results to a file and have a list of expected process name, the ones that don't match send an email saying

Alert Process:
Apple-TE--03
Orange-TE--02
Are down


Not really sure how to do that. Any help would be greatly appreciated.

Thanks!

Answer

With a Perl script

use warnings 'all';
use strict;

my @watch = qw(Orange-TE--02 Apple-JI--01 Orange-JI--06);

# Extract second column from the table of running processes, drop header line
my @running = map { (split)[1] } `process.sh -s`; 
shift @running;

my @down;
foreach my $proc_name (@watch) {
    push @down, $proc_name  if not grep { /^$proc_name$/ } @running;
}

if (@down) { 
    print "$_ is down\n" for @down;
    # send email
}

I don't know how this should be monitored but you can put it in a loop such as

for (1..$max_time_steps) {
    # code above
    sleep 1;
}

Or you can have it inside of while (1) { ... } with a suitable condition for when to break out.

For emailing you can use one of a number of modules, or mailx or such (via system).