Re: ps in perl?
by McDarren (Abbot) on Dec 26, 2007 at 17:01 UTC
|
| [reply] |
|
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.)
| [reply] [d/l] |
|
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?)
| [reply] |
|
'corelist Proc::ProcessTable'
| [reply] |
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. | [reply] |
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. | [reply] |
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 - http://paul.buetow.org
| [reply] [d/l] |
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. | [reply] [d/l] |
|
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 | [reply] [d/l] |
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";
| [reply] [d/l] |
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
| [reply] |
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!
| [reply] |