Programmatically Getting Process ID

Bill Anderson banderson at boi.hp.com
Mon Mar 6 19:46:31 CET 2000


"Samuel A. Falvo II" wrote:
> 
> In article <89pi3n$8lc$1 at nnrp1.deja.com>, Hernan M. Foffani wrote:
> >The portable way to do it (in UN*X, I mean) is to run a script and get
> >the output (via pipes), something like:
> >
> >#!/bin/sh
> >ps -ef | grep "$1" | grep -v grep | awk '{ print $1 }'
> 
> I just got done doing this and, unfortunately, this is not portable.  This
> script will not return the PID of the process(es), but rather the user under
> which the said process(es) run.
> 
> On _my_ Linux distribution, the awk statement should read '{ print $2 }'
> instead of $1.  I'm not sure about how well this would work on a non-Linux
> system.

After changing it to $2, it works on my Linux and HPUX boxen.

I believe this _should_ be portable for Unixen. After all, the key in
this equation is ps's output format.

If not, you can call ps with a formatting option, whoch should
_theoritcally_ make it *nix portable. 
YMMV

> Just giving you a heads up.  

Ditto.

-- 
Bill Anderson 			Linux/Unix Administrator, Security Analyst
ESBU (ARC)			bill_anderson at boi.hp.com
My opinions are just that; _my_ opinions.



More information about the Python-list mailing list