Getting a proccesses ID

Martin v. Loewis martin at
Fri Sep 6 18:03:28 CEST 2002

Micah Mayo <astrophels at> writes:

> Can anyone tell me a cross platform way of grabbing a particular
> processes ID

Depends on how you want to identify the process. If the process is a
child of yours, you best safe the process id at the time of process

If the process is sshd, you read at /var/run/ If you don't
trust that sshd saves its pid file in /var/run, you read
/etc/sshd/sshd_config - it might be best to find out the location of at installation time.

If you *really* need to invoke ps, I recommend to
- perform the searching in Python, instead of invoking grep/awk.
- you plan for two families of ps binaries: SysV and BSD. On Solaris,
  ps x says
usage: ps [ -aAdeflcjLPy ] [ -o format ] [ -t termlist ]
        [ -u userlist ] [ -U userlist ] [ -G grouplist ]
        [ -p proclist ] [ -g pgrplist ] [ -s sidlist ]
  'format' is one or more of:
        user ruser group rgroup uid ruid gid rgid pid ppid pgid sid taskid
        pri opri pcpu pmem vsz rss osz nice class time etime stime
        f s c lwp nlwp psr tty addr wchan fname comm args projid project pset

  On Solaris, it so happens that you can use /usr/ucb/ps x, but that
  won't be available on other SysV systems. The SysV equivalent is "ps

- you plan for multiple sshd processes being found. In theory, you
  need to find the one that has "1" as its parent process, but killing
  the one with the lowest pid might be good enough in real life.


More information about the Python-list mailing list