Getting the PID

Getting the PID

Post by Pasqualino Cascian » Thu, 04 May 2000 04:00:00



Hello,

trying to get the PID a program (the first instance of it, to be
precise) I've come to this:

ps ax --sort=pid | grep <program_name> | awk -F " " '{print $1}' | sed -n '1p'

And now I am wondering wether there is a neater solution to do this.

Thanks.

Pasqualino

 
 
 

Getting the PID

Post by Matthew Land » Thu, 04 May 2000 04:00:00



> Hello,

> trying to get the PID a program (the first instance of it, to be
> precise) I've come to this:

> ps ax --sort=pid | grep <program_name> | awk -F " " '{print $1}' | sed -n '1p'

> And now I am wondering wether there is a neater solution to do this.

> Thanks.

> Pasqualino

ps ax --sort=pid |awk '/<programname>/{print $1; exit}'

 - Matt

--
_______________________________________________________________________

   << Comments, views, and opinions are mine alone, not IBM's. >>

 
 
 

Getting the PID

Post by Michael Sternber » Thu, 04 May 2000 04:00:00



> ps ax --sort=pid | grep <program_name> | awk -F " " '{print $1}' | \
>    sed -n '1p'

(a) you cannot rely upon the fact that grep has a higher pid than ps (which is
    the purpose of sed above)
(b) incomplete regex - consider "sh" vs. basically any other shell
(c) it should be perfectly possible to have more than one hit
(d) you MAY want to limit the search based upon UID

Besides, the mantra is:  identifying a process by name is not portable and not
safe.  A user process may change argv[0].

Quote:> And now I am wondering whether there is a neater solution to do this.

Without listing users [point d], and with SysV ps (!):

    ps -eo pid,command | awk '$2 ~ /^program$/ {print $1}'

This is robust enough for a kill script (as long as you have awk available at
that point -- you might need to descend to the = test in sh).  Alas, for the
command line, with "visual parsing", the everday screwdriver-type solution is:

    ps aux | grep program | grep -v grep

Regards,
--
Michael Sternberg                        | Uni-GH Paderborn
http://www.phys.uni-paderborn.de/~stern/ | FB6 Theoretische Physik
phone: +49-(0)5251-60-2329   fax: -3435  | 33098 Paderborn, Germany
"Who disturrrbs me at this time?"  << Zaphod Beeblebrox IV >>     <*>

 
 
 

1. How to make a RSH and getting is PID.

Is there a way I can do a RSH, then get his pid ?
I`m trying to program an exe that will start a RSH and then start a
process on the remonte computer.  There's a way to do that I'm sure.
What are the systems call I must do ?  Is it possible I can do it in one
command like

rsh  ( parameters ) | process_to_start

Thanks for your answer.

2. Searching for a WD7193 - Driver

3. getting process pid

4. Need help installing XFree86

5. problem getting the pid

6. Formatting 2 mirrored disks in parallel??

7. Getting the PID of a subshell

8. Help: how to remove Linux

9. getting a PID

10. Getting UTMP.pid file

11. Getting the pid of a process reading from a fifo

12. Getting a PID using cat

13. getting a pid in ksh