Re: Best way to check that a process is running on a Unix system?

Discussion in 'Python' started by =?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=, May 28, 2006.

  1. > What is the best way to check that a process is running (or better yet
    > number of instances) based on the name of the process? Would have to
    > work on a unix/linux system.


    Use "ps -C proc_name". Then either read the nr of lines in the output
    (for the number of instances) or read the return value. 0 if the
    process is running. Or read the /proc/*/status files and check if your
    process is in any of them.

    --
    mvh Björn
     
    =?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=, May 28, 2006
    #1
    1. Advertising

  2. BJörn Lindqvist <> wrote:
    > > What is the best way to check that a process is running (or better yet
    > > number of instances) based on the name of the process? Would have to
    > > work on a unix/linux system.

    >
    > Use "ps -C proc_name". Then either read the nr of lines in the output
    > (for the number of instances) or read the return value. 0 if the
    > process is running. Or read the /proc/*/status files and check if your
    > process is in any of them.


    Here is a module I wrote ages ago which does the grubbling around in
    /proc for you.

    >>> import process
    >>> p = process.ProcessList()
    >>> p.named("emacs")

    [Process(pid = 20610), Process(pid = 6076), Process(pid = 6113), Process(pid = 6590), Process(pid = 857), Process(pid = 1394), Process(pid = 28974)]
    >>>


    Parsing the output of "ps -ef" will be more portable between unixes
    though!

    ------------------------------------------------------------

    """
    Manage Processes and a ProcessList under Linux.
    """

    import os
    import signal

    class Process(object):
    """Represents a process"""
    def __init__(self, pid):
    """Make a new Process object"""
    self.proc = "/proc/%d" % pid
    pid,command,state,parent_pid = file(os.path.join(self.proc, "stat")).read().strip().split()[:4]
    command = command[1:-1]
    self.pid = int(pid)
    self.command = command
    self.state = state
    self.parent_pid = int(parent_pid)
    self.parent = None
    self.children = []
    def kill(self, sig = signal.SIGTERM):
    """Kill this process with SIGTERM by default"""
    os.kill(self.pid, sig)
    def __repr__(self):
    return "Process(pid = %r)" % self.pid
    def getcwd(self):
    """Read the current directory of this process or None for can't"""
    try:
    return os.readlink(os.path.join(self.proc, "cwd"))
    except OSError:
    return None

    class ProcessList(object):
    """Represents a list of processes"""
    def __init__(self):
    """Read /proc and fill up the process lists"""
    self.by_pid = {}
    self.by_command = {}
    for f in os.listdir("/proc"):
    if f.isdigit():
    process = Process(int(f))
    self.by_pid[process.pid] = process
    self.by_command.setdefault(process.command, []).append(process)
    for process in self.by_pid.values():
    try:
    parent = self.by_pid[process.parent_pid]
    #print "child",process
    #print "parent",parent
    parent.children.append(process)
    process.parent = parent
    except KeyError:
    pass
    def named(self, name):
    """Returns a list of processes with the given name"""
    return self.by_command.get(name, [])

    ------------------------------------------------------------


    --
    Nick Craig-Wood <> -- http://www.craig-wood.com/nick
     
    Nick Craig-Wood, Jun 5, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    12
    Views:
    1,682
    Dave Thompson
    Jan 10, 2005
  2. Arthur Pemberton
    Replies:
    1
    Views:
    368
    Eriol
    May 29, 2006
  3. Replies:
    18
    Views:
    644
    Dave Thompson
    Jan 10, 2005
  4. Carl Youngblood
    Replies:
    6
    Views:
    285
  5. Replies:
    14
    Views:
    261
    Martijn Lievaart
    Aug 15, 2009
Loading...

Share This Page