in reply to loop control
Or more condensed:#!/usr/bin/perl -w use strict; my @required = qw( CMD /bin/sh rds SysExec /oasis/bin/sysmenu /oasis/bin/TS_TextSrvcs ); # invoke ps -a using backticks, read result into a list # for each line, split on whitespace, keep 3rd element # return a list of element => undef pairs my %procs = map { (split ' ')[3], undef } qx/ps -a/; # %procs now has a key named after each process # values are empty, mere existence of the keys suffices # now for each required process, # check if it has an entry in the hash of running procs my @missing = grep !exists $procs{$_}, @required; # print the resulting list of missing processes print map "NF: $_\n", @missing;
Note however that most every variant of ps has some switch to make it output only process names, in which case the split becomes superfluous. Assuming GNU ps:#!/usr/bin/perl -w use strict; my %procs = map { (split ' ')[3], undef } qx/ps -a/; print map "NF: $_\n", grep !exists $procs{$_}, qw( CMD /bin/sh rds SysExec /oasis/bin/sysmenu /oasis/bin/TS_TextSrvcs );
my %procs = map { chomp; $_ => undef } qx/ps -a ho comm/;
This is much more robust, as it avoids the need to parse an external program's output.
All that said and done, if you want to monitor your system, you should not reinvent the wheel. Nagios, mon and Big Brother will do many things for you. Don't waste your time.
Makeshifts last the longest.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: loop control
by maxl90 (Sexton) on Feb 05, 2003 at 19:01 UTC |