Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.

ps in perl?

by pileofrogs (Priest)
on Dec 26, 2007 at 16:52 UTC ( [id://659067]=perlquestion: print w/replies, xml ) Need Help??

pileofrogs has asked for the wisdom of the Perl Monks concerning the following question:

Okay, I must be missing something really obvious, so apologies for a stupid question.

Is there a core perl subroutine that will give me a list of running processes? I'm looking for ps without forking ps.


Replies are listed 'Best First'.
Re: ps in perl?
by McDarren (Abbot) on Dec 26, 2007 at 17:01 UTC
    I'm not sure if it is core or not, but Proc::ProcessTable is probably what you want.

    The example in the module documentation even gives a "cheap and sleazy" version of ps.

    Darren :)

      Proc::ProcessTable is not core. If it were, this would tell you the first version of perl it was available with:

      perl -MModule::CoreList -e 'print Module::CoreList->first_release('Pro +c::ProcessTable'),"\n";'

      It's a unix-oriented module, which I would guess wouldn't tend to be included in core if only for that reason.

      (The OP, pileofrogs, hasn't made it clear if he's happy with a unix-only solution.)

        The immediate problem is not "is it core". The problem is, "do I have it", or, if not, "how hard is it to install". And the latter applies both to technical issues (how hard work is it to install, and does it even work on my platform), and political ones (will my sysadmin let me install it?)
        'corelist Proc::ProcessTable'
Re: ps in perl?
by jrsimmon (Hermit) on Dec 26, 2007 at 21:08 UTC
    Alternatively, if you are looking for something that runs on Win32, Win32::Process::List would appear to be a good candidate.
Re: ps in perl?
by schweini (Friar) on Dec 26, 2007 at 17:47 UTC
    if for some reason you don't want to use the aforementioned module, you could always poke around in the /proc directory of linux installations to find any kind of information concerning running processes the machine is aware of.
Re: ps in perl?
by buetow (Beadle) on Dec 26, 2007 at 23:48 UTC
    Hello! maybe this can be considered as well:
    use Shell qw(ps); my @args = ....; my @res = ps(@args);
    the Shell module is already in the perl core!
    Paul C. BŁtow -
Re: ps in perl?
by apl (Monsignor) on Dec 26, 2007 at 18:23 UTC
    Or, if you donm't like either of the previous solutions you could do

    system( 'ps -ef >/tmp/ps.txt' );

    After which you could load the contents of /tmp/ps.txt. It's not elegant, but it's simple and removes a dependency.
      Perl provides you with a mechanism for reading the output of a system command, via backticks or the qx operator, which means you can eliminate the temporary file and process the output directly:
      foreach my $line ( qx[ps -ef] ) { # find the $line you're looking for }
      I've used the Proc::ProcessTable module with success, and second McDarren's recommendation, though.

      -- Douglas Hunter
Re: ps in perl?
by Khen1950fx (Canon) on Dec 27, 2007 at 05:01 UTC
    Yet another way: If you just want the pid and psinfo for a script or program, then you could use Unix::Process by jettero
    #!/usr/bin/perl use Unix::Process; my $vsz = Unix::Process->vsz($$); my $ppid = Unix::Process->ppid; my $pid = Unix::Process->pid; my $tty = Unix::Process->tty; my $stat = Unix::Process->stat; my $start = Unix::Process->start; my $rss = Unix::Process->rss; my $command = Unix::Process->command; print "$vsz, $ppid, $pid, $tty, $stat, $start, $rss, $command\n";
Re: ps in perl?
by grinder (Bishop) on Dec 27, 2007 at 14:33 UTC

    If you're on FreeBSD, you can use BSD::Process, which offers a direct interface into the low-level kernel structures of the processes running in the system. It allows you to retrieve much additional information not available via ps.

    • another intruder with the mooring in the heart of the Perl

Re: ps in perl?
by pileofrogs (Priest) on Apr 04, 2008 at 16:17 UTC

    Wow, I totally suck. I asked this question, got boatloads of answers, and didn't even check back...

    I'll update in case someone else asks this same question or maybe just to complete the circle of carma...

    I don't need Windows compatability, but I need more than Linux. I run Linux and OpenBSD. So that rules out proc.

    The output of programs like ps can sometimes change over time and between Linux and OpenBSD, so I don't want to maintain the regexs to keep track of all that. If there were something like a POSIX process_list() c function that had an obvious perl wrapper, that would be ideal. It looks like there is no such thing though.

    Using a CPAN module is OK, but not ideal. I don't have any problems politically or technically, but every time I add another CPAN dependency, there's another link in the chain to break, so if it's something I can do reasonably without a CPAN module, I will.

    That being said, in this case using a CPAN modules is probaly the best idea. I can let someone else handle the headaches of keeping their module in sync with however the OS likes to present process info.

    Thanks everyone!

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://659067]
Approved by McDarren
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-20 03:02 GMT
Find Nodes?
    Voting Booth?

    No recent polls found