basic python-unix question

Quinn Dunkan quinn at yak.ugcs.caltech.edu
Wed Jul 18 18:38:54 EDT 2001


On Wed, 18 Jul 2001 21:51:25 GMT, Ed T <spi at micro.soft> wrote:
>Hi Folks,
>New to Python and have been unable to us the "ps ef |grep" command
>correctly. From what I can tell I need to use the os.system() command. But
>have had no luck. Here an example of a basic script:
>
>import os, sys
>process = ("test1", "test2")
>
>for eachProcess in process:
>     os.system('ps ef | grep eachProcess')
>
>
>The idea is to place processes that should be running into a for loop and
>see if it is running. When I do the above it appears only to check for the
>word "eachProcess" not for the process (i.e., test1 or test2). Is this a
>case of how to enter this command where substitution takes place in Python?

Yes.  Automatic substitution does not take place at all.  Strings will never
change their contents.

There are many functions that operate on strings, though.

You can concatenate strincgs with (+):
    'ps ef | grep ' + eachProcess

You can interpolate strings with (%):
    'ps ef | grep %s' % eachProcess

Read the docs for more.

As an aside, the above os.system is not a very good way to check for running
processes (you want a >/dev/null in there at least, not to mention that it
will break if you move it to a bsd).  I recommend finding out the relevant
pids and using os.kill(0, pid).



More information about the Python-list mailing list