Howto run a sub thread/process to monitor the system status in Python?

Posted on May 22, 2009. Filed under: Linux, Python |

Howto run a sub thread/process to monitor the system status in Python?

 1. popen  or popen2

 This will call another process by /bin/sh, so if you want get the pid, the result pid maybe the pid of /bin/sh

 Examples:

1.1   without log

>>>Import popen2, os

>>>Cur = popen2.Popen4(“vmstat –n 100”)

>>>Cur.pid

123

>>> 

 

ps aux|grep vmstat

root       123    0.0  0.1   1808   564 pts/9    S+   10:57   0:00 vmstat -n 100

 

1.2    With log

>>>import popen2, os

>>>cur = popen2.Popen4(“vmstat –n 100 > vmstat.log”)

>>>cur.pid

124

>>> 

 

ps aux|grep vmstat

root       124  0.1  0.2   4488   996 pts/9    S+   11:01   0:00 /bin/sh -c vmstat -n 100 > vmstat.log

root       125  0.0  0.1   1804   560 pts/9    S+   11:01   0:00 vmstat -n 100

 

>>>os.kill(cur.pid, 9)

>>>os.waitpid(cur.pid,0)

(124, 9)

>>> 

 

ps aux|grep vmstat

root       125  0.0  0.1   1804   560 pts/9    S+   11:01   0:00 vmstat -n 100

 

From the examples, you can see that, if you want the sub-thread record some log from the command, you’d better do not use this kind of popen2 lib, you can use following method

 

2. subprocess

 

2.1 Start the subprocess:

>>>Outlog = open(outputlogname, “w”)

>>>errlog=open(errlogname, “w”)

>>>try:

>>>    process=subprocess.Popen([“vmstat”,”-n”,”100”], stdout=outlog, stderr=errlog)

>>>except  OSError,e:

>>>    print “The OSError is:”,e

>>>    print “Maybe  this command is not exist”

>>>except Exception,e2:

>>>    print e

>>>pid = process.pid

134

 

ps aux|grep vmstat

root       134  0.0  0.1   1804   560 pts/9    S+   11:01   0:00 vmstat -n 100

 

You can see here: there is no “/bin/sh” process, the Popen have a parameter “shell=True/False”:

On Unix, with shell=False (default): In this case, the Popen class uses os.execvp() to execute the child program. args should normally be a sequence. A string will be treated as a sequence with the string as the only item (the program to execute).

On Unix, with shell=True: If args is a string, it specifies the command string to execute through the shell. If args is a sequence, the first item specifies the command string, and any additional items will be treated as additional shell arguments.

 

2.2 Kill the sub process

>>>os.kill(process.pid, 9)

134

>>>os.waitpid(process.pid, 0)

(134, 9)

>>>outlog.close()

>>>errlog.close()

 

 

 

Reference:

http://mail.python.org/pipermail/python-list/1999-May/002214.html

http://docs.python.org/library/subprocess.html

http://docs.python.org/library/popen2.html

http://mail.python.org/pipermail/python-list/2004-May/260937.html

http://code.activestate.com/recipes/496960/

http://www.daniweb.com/forums/thread36752.html#

http://groups.google.com/group/comp.lang.python/msg/9fa3a3c287e8e2a3?hl=en&

http://code.activestate.com/recipes/52296/

Advertisements

Make a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Liked it here?
Why not try sites on the blogroll...

%d bloggers like this: