[Tutor] commands module

Evert Rol evert.rol at gmail.com
Fri Dec 7 00:31:46 CET 2007


> 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



More information about the Tutor mailing list