Beginner asks question about Python on Linux

Preston Landers prestonlanders at my-deja.com
Fri Oct 8 10:37:54 EDT 1999


Hello Stephen,

you might try something like this:

import commands
import string

status, output = commands.getstatusoutput("ps aux|grep someProcess")

if status:
    print "Sorry, grep could not find anything."
else:
    lines_list = string.split(output, "\n")
    pids_list = []
    for line in lines_list:
        pids_list.append(string.split(line)[1])

Hope this helps,

---Preston


In article <7tkge6$asd$1 at nnrp1.deja.com>,
  stephen.allison at cenes.co.uk wrote:
> Hello all,
>
> I have a, probably fairly trivial, question about using Python on
Linux
> (and probably Unix too).  I'm quite new to both, so apologies if it's
a
> really stupid question.
>
> I'm writing some scripts to do some general administation tasks, and
> need to get the output of a shell command into Python easily.  For
> example I have a bit of code like this:
>
> os.system("ps aux|grep someProcess > pids.temp")
>
> I then proceed to open the pids.temp file, read the lines and then go
> on to do stuff with the processes (readline, splitfields, look through
> the list for the PIDs, execute further system commands (usually
> 'kill')), and then delete the pids.temp file.  What I want to know is
> if there is a way of getting the output of the grep straight into
> Python, so I won't have to use a temporary file.  I have since learned
> about killing all processes of a given name, so it's a bit academic,
> but I was wondering if anyone could tell me if it's possible to do
> this, using either Python cleverness or some shell commands I'm not
> aware of.
>
> Thanks for any help,
>
> Steve
>
> --
> Steve Allison
> Doesn't read deja news mail
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.
>

--
|| Preston Landers <prestonlanders at my-deja.com> ||


Sent via Deja.com http://www.deja.com/
Before you buy.




More information about the Python-list mailing list