Perl equivalent for Unix ps.

P

Prab_kar

Hi all,
Is there any Perl function which works like the Unix ps command?
I've looked through the manuals for Perl functions and couldnt find
anything similar in nature.

I'm rewriting a Bourne shell script in Perl and the sh script uses the
ps from a lot of different places, /usr/bin/ps sometimes, /usr/ucb/ps
other times. I wanted to rewrite in Perl to get over that, and in all
the examples I see, the Perl scripts use /usr/bin/ps or /usr/ucb/ps in
backticks or from system("<PATH>/ps").

Is there anyway I can use Perl's internal functions to identify/monitor
the process and stop/kill them?
I realize there's Proc::processTable .pm for this, but I want my
distribution to include just the Perl script and have it do it all.
I'm working on Solaris, Perl 5.6.

Thanks for your time,
Prabh
 
G

Gregory Toomey

Hi all,
Is there any Perl function which works like the Unix ps command?
I've looked through the manuals for Perl functions and couldnt find
anything similar in nature.

I'm rewriting a Bourne shell script in Perl and the sh script uses the
ps from a lot of different places, /usr/bin/ps sometimes, /usr/ucb/ps
other times. I wanted to rewrite in Perl to get over that, and in all
the examples I see, the Perl scripts use /usr/bin/ps or /usr/ucb/ps in
backticks or from system("<PATH>/ps").

Is there anyway I can use Perl's internal functions to identify/monitor
the process and stop/kill them?
I realize there's Proc::processTable .pm for this, but I want my
distribution to include just the Perl script and have it do it all.
I'm working on Solaris, Perl 5.6.

Thanks for your time,
Prabh

If you dont want to use ProcessTable.pm, then in linux I use the output from
ps:

sub killem {
my($pid,$tname,$etime,$cmd,$emin,$ehour,$background);

for (split '\n', qx(ps -u $> -o pid,tname,etime,cmd --no-headers)) {
($pid, $tname, $etime,$cmd) = unpack "a5 x a11 x a8 x a200"
$_;
#print "$_\n";
$etime =~ /((\d*):)?(\d*):(\d*)$/;
$ehour = $2;
$emin = $3;
$background=/\?/;
next if $pid==$$;

if ($ehour>12 || ((!/sshd/)&&$background&&($ehour>=1|
$emin>10))) {
log('Killing '.$_);
kill 9,$pid;
}
}
}

gtoomey
 
P

Prab_kar

Thanks for your response, Jim and Greg.

My problem is I want to use pure Perl to do this.
I dont want to get into the whole platform check routine of,
If Linux
Use ps from /bin/ps
Else if Solaris
Use ps from /usr/bin/ps or /usr/ucb/ps

I wanted to see if pure Perl can give me the same info that the running
ps from backticks or system("ps") would give.
Unfortunately, that doesnt seem to be the case.
Thanks for your time,
Prabh
 
J

John W. Krahn

Michael said:
what's wrong with opening a filehandle (e.g.

open (PS,"ps -ef") && die "can't open ps: $!\n";

Did you try that? When you try to open the file 'ps -ef' what happens?


John
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top