Perl equivalent for Unix ps.

Discussion in 'Perl Misc' started by Prab_kar@hotmail.com, Jan 30, 2005.

  1. Guest

    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
     
    , Jan 30, 2005
    #1
    1. Advertising

  2. Jim Keenan Guest

    wrote:
    > 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.
    >


    See the discussion here (which, unfortunately, rains on your parade):

    http://search.cpan.org/~nwclark/perl-5.8.6/lib/Shell.pm#Caveats

    jimk
     
    Jim Keenan, Jan 31, 2005
    #2
    1. Advertising

  3. wrote:

    > 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
     
    Gregory Toomey, Jan 31, 2005
    #3
  4. Guest

    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
     
    , Jan 31, 2005
    #4
  5. Michael Vilain wrote:
    > In article <>,
    > wrote:
    >>
    >>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.

    >
    > 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
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Jan 31, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,829
  2. Dan Jacobson

    do the equivalent of unix date(1) in C

    Dan Jacobson, Oct 31, 2003, in forum: C Programming
    Replies:
    2
    Views:
    378
    Peter Shaggy Haywood
    Nov 3, 2003
  3. Robert Wallace

    my own perl "dos->unix"/"unix->dos"

    Robert Wallace, Jan 21, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    293
    Michele Dondi
    Jan 22, 2004
  4. Mike

    Perl equivalent to unix script

    Mike, Oct 30, 2006, in forum: Perl Misc
    Replies:
    11
    Views:
    423
    Peter J. Holzer
    Nov 3, 2006
  5. Melroy

    Perl equivalent of unix time command

    Melroy, Sep 7, 2008, in forum: Perl Misc
    Replies:
    13
    Views:
    214
Loading...

Share This Page