The way you assign a number as a key for each element of %prolist seems pointless, if I read your intentions correctly. Here's what you're probably trying to do:
#!/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;
Or more condensed:
#!/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 );
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:
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.


In reply to Re: loop control by Aristotle
in thread loop control by maxl90

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.