strange subprocess behavior when calling ps

Chris Rebert crebert at ucsd.edu
Tue Nov 16 22:13:16 EST 2010


On Tue, Nov 16, 2010 at 6:33 PM, Roger Davis <rbd at hawaii.edu> wrote:
> Hi all,
>
> I have encountered a strange problem with some code I am writing to
> search the system process list for certain running processes. I am
> using subprocess.Popen() to call '/bin/ps -e'. When I save my code to
> the file pid.py (whose first line is #!/usr/bin/python) and run it
> with the command
>
> % ./pid.py
>
> it works perfectly fine, retrieving lines from my pipe to the /bin/ps
> output which look exactly as if I had typed the command '/bin/ps -e'
> myself into a shell window. Here is a sample line from that output:
>
>  1891 ttys000    0:00.12 -tcsh
>
> Now for the weird part -- when I run this code using the command
>
> % python pid.py
>
> I get entirely different output. It only prints out a very few
> processes instead of the entire table, and each line also has lots of
> environment variable values displayed.
<snip>
> It's like it's calling up an entirely different ps, or passing it
> different command arguments. In both cases, however, I am explicitly
> calling /bin/ps with the same -e argument, and there appear to be no
> other ps commands on my system, neither do I appear to have any ps
> builtin command in any shell.
>
> I am running 2.6.6 under MacOS 10.6.4 on a MacBook Pro Intel. I have
> appended the code below. I am running both commands directly in a
> Terminal window running tcsh.
>
> Can anyone explain this? Thanks!
<snip>
> ##### code follows
>
> #!/usr/bin/python
<snip>

Have you checked whether those commands are running under the same
Python? What output do you get from tcsh for the following?:
which python
python -V
/usr/bin/python -V
ls -l /usr/bin/python

Also, did you upgrade your system Python or something? I'm running Mac
OS 10.6.5 and the built-in /usr/bin/python is v2.6.1, so I find the
implied claim that your /usr/bin/python is v2.6.6 to be rather
bizarre.

I am unable to reproduce your problem with either my v2.6.1 system
Python or my v2.6.6 Python from Fink.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list