RE: check process state
you could use the Proc::ProcessTable module -Original Message- From: Sylvanie, Jean-Pierre [mailto:[EMAIL PROTECTED]] Sent: Monday, September 30, 2002 11:39 AM To: '[EMAIL PROTECTED]' Subject: check process state Hi guys, I want to do a sub that check if a process is sleeping or not... I wrote the following sub, but I was wondering if it was possible to to it without shell calls... Thanks, jp. sub isSleeping{ # get PID of process to check my $pid = shift || return 0; # get user name my $user; if(`id` =~ /\((\w+)\)/) {$user=$1}; # the 7th field (starting at 0) of top output is the state. # top -b (for batch mode) -U $user (processes of $user). my $state = (split /\s+/, (grep {/$pid/} `top -b -U $user`)[0])[7]; return $state eq sleep; } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: check process state
On Mon, 30 Sep 2002 11:38:40 -0400, [EMAIL PROTECTED] (Jean-Pierre Sylvanie) wrote: Hi guys, I want to do a sub that check if a process is sleeping or not... I wrote the following sub, but I was wondering if it was possible to to it without shell calls... My top command dosn't allow for -U or $user. Maybe you meant to use ps ? sub isSleeping{ # get PID of process to check my $pid = shift || return 0; # get user name my $user; if(`id` =~ /\((\w+)\)/) {$user=$1}; # the 7th field (starting at 0) of top output is the state. # top -b (for batch mode) -U $user (processes of $user). my $state = (split /\s+/, (grep {/$pid/} `top -b -U $user`)[0])[7]; return $state eq sleep; } Try this Proc::ProcessTable method: ## #!/usr/bin/perl -w use strict; use Proc::ProcessTable; my $t = new Proc::ProcessTable; foreach my $p ( @{$t-table} ){ print $p-pid,\t,$p-state,\t,$p-cmndline,\n; } ### -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: check process state
Jean-Pierre Sylvanie wrote: Hi guys, I want to do a sub that check if a process is sleeping or not... I wrote the following sub, but I was wondering if it was possible to to it without shell calls... Thanks, jp. sub isSleeping{ # get PID of process to check my $pid = shift || return 0; # get user name my $user; if(`id` =~ /\((\w+)\)/) {$user=$1}; # the 7th field (starting at 0) of top output is the state. # top -b (for batch mode) -U $user (processes of $user). my $state = (split /\s+/, (grep {/$pid/} `top -b -U $user`)[0])[7]; return $state eq sleep; } i don't understand why people keep using 'ps' or 'top' and then do a bunch of regular expression to look for a program/process that they want. this is so inefficient(more coding especially the reg. portion), unsafe(if $user happens to be: 'whatever;rm -fr /*' and you are root, your system is gone, forever!), not portable(not every system use the exact same display for top or ps. what happen if all the ps or top change the display format? are you all going to re-do all of your reg. expression to match the new format? another change, another re-do?). sorry about the complian! it's my only 2cent of the day. i promise. :-) please check out the Proc::ProcessTable in CPAN. it does everything you ask for and much more such as 'give me all process that's using more than 50% of the system memory' or 'give me a list of process that have sleep for more than 5 minutes'... etc. david -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: check process state
Zentara wrote: My top command dosn't allow for -U or $user. Maybe you meant to use ps ? the -U argument only exist for a certain version of top Top version 3.4 has it in my UNIX machine in my Linux box: [david@panda]$ top -V top (procps version 2.0.7) doesn't have it. david -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]