Python help...

Jonathan Ellis jonathan at carnageblender.com
Wed Oct 5 14:48:34 MDT 2005


On Wed, 05 Oct 2005 13:56:55 -0600, "Mitch Anderson"
<mitch at metauser.net> said:
> My code is simple:
> 
> import commands
> print commands.getoutput("/usr/bin/sar 0 | grep all | awk '{ print
> $4\"#\"$5\"#\"$6\"#\"$7\"#\"$8 }'")
> 
> problem is the results:
> 0.84#0.40#0.36#0.23#98.17
> Cannot write data to system activity file: Broken pipe
> 
> I'm not sure where its trying to write anything... as the command
> executed on the command line produces no errors..

That's a sar message.  Apparently it doesn't like being run as a
subprocess, for some reason.

The commands module is kind of old and busted.  Use subprocess instead,
which allows you to get stdout unmingled with stderr:

import subprocess
p = subprocess.Popen('sar 0 |grep -v foo', shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p.stdout.read()

(The UPyUG's october meeting will cover "python for sysadmins:"
http://utahpython.org.)

-Jonathan



More information about the PLUG mailing list