connollyc4 connollyc4 - 4 months ago 11
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::


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' -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:
Are down

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



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] } ` -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).