[Tutor] commands module (Forwarded back to list)

ds ds-python-tutor at sidorof.com
Fri Dec 7 02:58:58 CET 2007


Evert Rol wrote:

>> >> I've been trying to do something that I thought was going to be
>> >> relatively straight-forward, but so far I haven't found a good solution.
>> >>
>> >> What I'm trying to do is discover a pid on a process and kill it.  The
>> >> way that I thought that I could do it is something along the lines of:
>> >>
>> >> import commands
>> >>
>> >> program = "someprogram"
>> >>
>> >> a = commands.getoutput('ps ax|grep %s ' % (program))
>> >>
>> >> Then, I'd parse the returned info get the pid and kill it, probably via
>> >> another command.
>> >>
>> >> However, what happens is that the "ps ax" portion truncates the listing
>> >> at 158 characters.  It just so happens that the unique name that I need
>> >> in the list comes after that.  So, works from the bash shell, but
>> >> doesn't work using getoutput.
>>     
> >
> > What's the result of getoutput(); ie, what is a?
> > Note that bash and commands.getoutput() are not the same, since the
> > latter executes 'sh -c', which is slightly different. I don't expect
> > that'll solve your problem.
> > Does the -w option help? I'm guessing it won't, since the truncation
> > seem to be due to some odd character causing an EOF or something (I
> > tried myself here, both on Linux & OS X, without problems).
> >
> >   Evert
>   


I accidentally sent this directly rather than to the list:

Thanks for your reply.  When I said 158 characters I was trying to say
each _line_ of the shell command "ps ax" was truncated to 158
characters, not that the _total_ returned was 158. 

Your question got me thinking about it, and I found in my set variables:
COLUMNS=158, which corresponds pretty well.
So, I tried setting COLUMNS equal to 500 (arbitrarily large) prior to
going into python.  It seems to change back to 158 automatically however.
For example, when I go into python, import commands, and execute
commands.getoutput('set') I find that COLUMNS is back to 158.  So, I
think my problem is that I don't know how to alter the set variable so
that it will stick long enough for the "ps ax" command to execute properly.


==========================
End of forwarded message part.


Finally, I have solved the problem, because I discovered a width option on the ps command, which I hadn't been aware of before.

For example:

commands.getstatusoutput('ps ax -l --width 500')

works very well by over-riding any defaults.

Thanks for your help.

ds






More information about the Tutor mailing list