Extracting PIDs from a 'ps aux' 
Author Message
 Extracting PIDs from a 'ps aux'

I am needing to create a script in UNIX that can be run from a cron job.
What this needs to do is:

    >  ps aux | grep radius

Which should generate this sort of output:

   root     16099  0.0  0.1    404   192   p5  S     9:02AM    0:17.82
radiusd
   root     16101  0.0  1.5  3936 3772  p5  S     9:02AM    0:24.63 radiusd

What I need to do is generate a 'kill -9 XXXXX' command to the command line,
where XXXXX is the pid of the processes running, which in the output is the
second field (16099 and 16101).

I have tried a few approaches, the most recent being something along the
lines of:




The problem I am running into, is that when using backquotes, multiple line
output is assigned an array as one line per element.  What do I need to do
to then extract the pids (second 'word' in each 'element') into an array

I can then do a:


at the end of the script.

Input, thoughts, and any comments are welcome, although you will have to
remove the NOSPAM string from my email addresses to respond.

John Jones
Systems Administrator
Paracom Technologies/Feist Internet
Wichita, KS






Fri, 30 Nov 2001 03:00:00 GMT  
 Extracting PIDs from a 'ps aux'

[snip]

Quote:





>The problem I am running into, is that when using backquotes, multiple line
>output is assigned an array as one line per element.  What do I need to do
>to then extract the pids (second 'word' in each 'element') into an array

>I can then do a:


>at the end of the script.

try using map with split, i.e.

That worked for my simple test, look in the perlfunc man page for
more details on map and split.

--
-------------------------------------------------------------------------------
     Gregory L. Snow         |   Imagination is the one weapon in the
     (Greg)                  |  war against reality.



Fri, 30 Nov 2001 03:00:00 GMT  
 Extracting PIDs from a 'ps aux'

Quote:

>     >  ps aux | grep radius
> Which should generate this sort of output:
>    root     16099  0.0  0.1    404   192   p5  S     9:02AM    0:17.82
> What I need to do is generate a 'kill -9 XXXXX' command to the command line,
> where XXXXX is the pid of the processes running, which in the output is the
> second field (16099 and 16101).
> I have tried a few approaches, the most recent being something along the
> lines of:


You mean


right?

Uh, I guess. But you're just putting a single entry into each array,
since the stuff on the right side of the equal sign in each case is

Quote:
> The problem I am running into, is that when using backquotes, multiple line
> output is assigned an array as one line per element.  What do I need to do
> to then extract the pids (second 'word' in each 'element') into an array


You would probably want to use the split function, say like this:

at which point $line1[1] is going to be your PID.

But this is clunky enough that even an idiot like me can see a cleaner
way to do it:



Quote:
}

or, more tersely, using $_, and the default behavior of split:



Quote:
}


foreach (`ps aux | grep radius`) {

Quote:
}
> I can then do a:

> at the end of the script.

Or, to take this to extremes, it can all go on one line:

foreach (`ps aux | grep radius`) { kill 9, (split)[1] }

I'm sure there are any number of obvious issues I've missed here, but
one thing I wonder about is the fact that the process ID of the grep
command you ran in the backticks will be included in the list of PIDs
to kill (since 'grep radius' will catch the 'radius' in its own ps
listing). I'm guessing that will not be a big deal, since that process
will be finished by the time the kill is issued anyway, but it's only a
guess, and I clearly don't know what I'm talking about here.

Someone with vastly more wisdom will doubtless address that point if it
merits addressing...

--
John Callender

http://www.west.net/~jbc/



Fri, 30 Nov 2001 03:00:00 GMT  
 Extracting PIDs from a 'ps aux'

Quote:

> I am needing to create a script in UNIX that can be run from a cron job.
> What this needs to do is:

>     >  ps aux | grep radius

There is also a module, Proc::ProcessTable, which
might be helpful for you.

--John Nolan



Fri, 30 Nov 2001 03:00:00 GMT  
 Extracting PIDs from a 'ps aux'

Quote:

> try using map with split, i.e.


Dang it; I *knew* there was a more concise approach I was overlooking.

I don't know why, but I have some kind of mental block that prevents me
from thinking of map as a solution to problems like this. The same
thing with the grep function; I just never seem to think of it until I
see someone else using it. You can imagine, then, how the Schwartzian
Transform made my eyes cross when I first saw it...

I guess it's because I devised workarounds involving foreach'ing over
the arrays before I knew that grep and map existed, and those have
served me well enough that I never internalized the more efficient
approach given above.

*sigh*

One of these days I'll start thinking in Perl...

--
John Callender

http://www.west.net/~jbc/



Sat, 01 Dec 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Accessing /dev/kmem (a la 'ps')

2. pshier: Hierarchical 'ps' in perl

3. Perl won't compile on AUX

4. How to extract data in 'groups'

5. Porting perl scripts to WinNT that use 'df' and 'ps'

6. Parsing unix process id's from ps?

7. Q: $0 doesn't change values returned by ps

8. Perl on aix 4,1.3 - can't get stty to effect ps command output

9. Extracting data from a ps command

10. What is pseudorandommy @chars=('a'..'z','A'..'Z','0'..'9','_');

11. can't extract frames

12. extracting text out of pdf's?

 

 
Powered by phpBB® Forum Software