Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Best way to check that a process is running on a Unix system?

Reply
Thread Tools

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

 
 
=?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=
Guest
Posts: n/a
 
      05-28-2006
> 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
 
Reply With Quote
 
 
 
 
Nick Craig-Wood
Guest
Posts: n/a
 
      06-05-2006
BJörn Lindqvist <(E-Mail Removed)> 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 <(E-Mail Removed)> -- http://www.craig-wood.com/nick
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Perl process as a unix background process gbostock@excite.com Perl Misc 14 08-15-2009 02:41 PM
Best way to check that a process is running on a Unix system? Arthur Pemberton Python 1 05-29-2006 01:50 AM
Show up process indicator while process is running then do page redirection Jona ASP .Net 2 09-01-2005 04:03 AM
Best way to determine if running on windows or unix-based system Carl Youngblood Ruby 6 07-04-2004 03:49 AM



Advertisments