in reply to Searching strings within an array for a particular word.
How's this?
#!/usr/bin/perl -w use strict; sub is_running { my $process = shift; # name of process my @p = grep { !/grep/ } `ps aux | grep $process`; # ps aux without the greps print @p; # comment this out return scalar @p; # false if @p is empty; true otherwise } # test program foreach (qw(httpd sshd syslogd telnetd ftpd yourmama)) { printf("$_ %s running.\n", (is_running($_) ? "is" : "is not")); }
w/ the experimental syntax-highlighted remix
#!/usr/bin/perl -w use strict; sub is_running { my $process = shift; # name of process my @p = grep { !/grep/ } `ps aux | grep $process`; # ps aux without the greps print @p; # comment this out return scalar @p; # false if @p is empty; true otherwise } # test program foreach (qw(httpd sshd syslogd telnetd ftpd yourmama)) { printf("$_ %s running.\n", (is_running($_) ? "is" : "is not")); }
|
---|
In Section
Seekers of Perl Wisdom