getting properly one subprocess output

Paul Rudin paul.nospam at rudin.co.uk
Thu Nov 19 10:55:09 EST 2009


Jean-Michel Pichavant <jeanmichel at sequans.com> writes:

> Hi python fellows,
>
> I'm currently inspecting my Linux process list, trying to parse it in
> order to get one particular process (and kill it).
> I ran into an annoying issue:
> The stdout display is somehow truncated (maybe a terminal length
> issue, I don't know), breaking my parsing.
>
> import subprocess
> commandLine = ['ps', '-eo "%p %U %P %y %t %C %c %a"']
> process = subprocess.Popen(commandLine, stdout=subprocess.PIPE,
> stderr=subprocess.PIPE)
> processList, stderrdata = process.communicate()
>
> Here is a sample of what I get in processList.split('\n'):
>
> ' "25487 1122      4344 ?         7-17:48:32  2.5 firefox-bin
> /usr/lib/iceweasel/firefox-"',
> ' "25492 1122      4892 pts/6          00:08 57.2 ipython
> /usr/bin/python /usr/bin/ip"',
>
>
> As you can see, to complete process command line is truncated.
> Any clue on how to get the full version ?
>

You need to pass -ww to ps, otherwise it tries to guess the width of
your terminal and adjust output line lengths accordingly.




More information about the Python-list mailing list