Perl interface to Unix ps?

J

J Krugman

I'm writing a Perl script that is supposed to find the PIDs of all
the running versions of a program and send them a SIGUSR1. As this
description suggests, the script is meant for a Unix platform, but
it could be Linux, BSD, Solaris, etc.

The only way I can think of to get the PIDs for all the desired
processes is to scan the output of a suitable 'system "ps -blah"'
call, but there are many versions of ps around, each with its own
syntax and output format. Does anyone know of a Perl interface
for ps? (I searched perldoc -q ps, man POSIX, and CPAN for ps but
didn't find anything; I'm hoping that what I'm looking for exists
in some less obvious place.)

TIA,

jill
 
C

chris-usenet

J Krugman said:
I'm writing a Perl script that is supposed to find the PIDs of all
the running versions of a program and send them a SIGUSR1. As this
description suggests, the script is meant for a Unix platform, but
it could be Linux, BSD, Solaris, etc.

As the risk of upsetting others in clpm, since it's for a *IX platform,
why write this in perl? This will send SIGUSR1 to any process called
"program":

kill -USR1 `ps -e | awk '$NF == "program" {print $1}'`

YMMV depending on which variant of ps you have (i.e. whether you're on
a SysV or BSD derived platform), but then the perl alternatives seem to
struggle with platform dependence, too, as they appear to depend on the
existence of /proc.

Chris
 
K

Keith Keller

There is also the Unix killall utility:

killall -USR1 programname

Apparently killall does not behave the same way on every OS: there are
some notes on Solaris killall, for example, that suggest it's a bit more
extreme (it kills all processes, thus killall).

--keith
 
D

DominiX

ici même:J Krugman said:
I'm writing a Perl script that is supposed to find the PIDs of all
the running versions of a program and send them a SIGUSR1. As this
description suggests, the script is meant for a Unix platform, but
it could be Linux, BSD, Solaris, etc.
....

TIA,

jill

for that purpose I use Proc::processTable;

HTH

-- dominix
 
M

Mladen Gogala

I'm writing a Perl script that is supposed to find the PIDs of all
the running versions of a program and send them a SIGUSR1. As this
description suggests, the script is meant for a Unix platform, but
it could be Linux, BSD, Solaris, etc.

#!/usr/bin/perl -w
use strict;
my @proclist=();

open BS,"ps -ef|" or die "Cannot open BS:$!\n";
while (<BS>) {
my @fields=split /\s+/;
push @proclist,$fields[1];
}
kill 10, @proclist
$
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top