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]

Reply via email to