Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > getting properly one subprocess output

Reply
Thread Tools

getting properly one subprocess output

 
 
Jean-Michel Pichavant
Guest
Posts: n/a
 
      11-18-2009
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 ?

JM

(python 2.5)
 
Reply With Quote
 
 
 
 
Jon Clements
Guest
Posts: n/a
 
      11-18-2009
On Nov 18, 11:25*am, Jean-Michel Pichavant <(E-Mail Removed)>
wrote:
> 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 ?
>
> JM
>
> (python 2.5)


What about "ps -eo pid,tty,cmd" ?

Sample:
12680 ? geany /usr/share/gramps/ReportBase/
_CommandLineReport.py
12682 ? gnome-pty-helper
12683 pts/0 /bin/bash
13038 ? gnome-terminal
13039 ? gnome-pty-helper
13040 pts/1 bash
13755 pts/1 ps -eo pid,tty,cmd

....etc...


hth,

Jon.



 
Reply With Quote
 
 
 
 
Jon Clements
Guest
Posts: n/a
 
      11-18-2009
On Nov 18, 4:14*pm, Jon Clements <(E-Mail Removed)> wrote:
> On Nov 18, 11:25*am, Jean-Michel Pichavant <(E-Mail Removed)>
> wrote:
>
>
>
> > 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 ?

>
> > JM

>
> > (python 2.5)

>
> What about "ps -eo pid,tty,cmd" ?
>
> Sample:
> 12680 ? * * * *geany /usr/share/gramps/ReportBase/
> _CommandLineReport.py
> 12682 ? * * * *gnome-pty-helper
> 12683 pts/0 * */bin/bash
> 13038 ? * * * *gnome-terminal
> 13039 ? * * * *gnome-pty-helper
> 13040 pts/1 * *bash
> 13755 pts/1 * *ps -eo pid,tty,cmd
>
> ...etc...
>
> hth,
>
> Jon.


Another thought: if you're only wanting to find and kill a process,
what about pkill? Saves you having to filter the list in Python and
then issue a kill command.

Jon.
 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      11-18-2009
On Wed, 18 Nov 2009 12:25:14 +0100, Jean-Michel Pichavant wrote:

> 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.


> As you can see, to complete process command line is truncated.
> Any clue on how to get the full version ?


If you only need it to work on Linux, you can just enumerate
/proc/[1-9]*/exe or /proc/[1-9]*/cmdline.

Or you can add -ww to "ps" to avoid truncating the output.

Note that the /proc/*/exe report the actual executable. The command line
reported by "ps" (from /proc/*/cmdline) can be modified by the program, so
it doesn't necessarily reflect the program being run.


 
Reply With Quote
 
Jean-Michel Pichavant
Guest
Posts: n/a
 
      11-19-2009
Nobody wrote:
> On Wed, 18 Nov 2009 12:25:14 +0100, Jean-Michel Pichavant wrote:
>
>
>> 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.
>>

>
>
>> As you can see, to complete process command line is truncated.
>> Any clue on how to get the full version ?
>>

>
> If you only need it to work on Linux, you can just enumerate
> /proc/[1-9]*/exe or /proc/[1-9]*/cmdline.
>
> Or you can add -ww to "ps" to avoid truncating the output.
>
> Note that the /proc/*/exe report the actual executable. The command line
> reported by "ps" (from /proc/*/cmdline) can be modified by the program, so
> it doesn't necessarily reflect the program being run.
>
>
>

That is what I was searching for. It's in ps man page, I don't know why
I missed it in the first place.
Thanks.

JM
 
Reply With Quote
 
Bas
Guest
Posts: n/a
 
      11-19-2009
On Nov 18, 12:25*pm, Jean-Michel Pichavant <(E-Mail Removed)>
wrote:
> 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.


Below is the script I use to automatically kill firefox if it is not
behaving, maybe you are looking for something similar.

HTH,
Bas


#!/usr/bin/env python

import commands, os
lines = os.popen('ps ax|grep firefox').readlines()
lines = [line for line in lines if 'grep' not in line]
print lines[0]
pid = int(lines[0][:5])
print 'Found pid: %d' %pid
os.system('kill -9 %d' %pid)
 
Reply With Quote
 
Paul Rudin
Guest
Posts: n/a
 
      11-19-2009
Jean-Michel Pichavant <(E-Mail Removed)> 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.

 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      11-19-2009
On Thu, 19 Nov 2009 06:21:09 -0800, Bas wrote:

> Below is the script I use to automatically kill firefox if it is not
> behaving, maybe you are looking for something similar.


> lines = os.popen('ps ax|grep firefox').readlines()


This isn't robust. It will kill any process with "firefox" anywhere in its
command line, which isn't limited to processes which are actually running
the firefox web browser.

> lines = [line for line in lines if 'grep' not in line]


This line excludes one such process, but there may be others.

A more robust approach would be to check for the string in the command
name (i.e. argv[0]) rather than the complete command-line, by using e.g.
"ps ... -o pid,comm":

lines = os.popen('ps axheo pid,comm').readlines()
lines = [line.strip().split(' ', 1) for line in lines]
lines = [(int(pid), cmd) for pid, cmd in lines if 'firefox' in cmd]

Better still would be to check that "firefox" is a complete word, not part
of one, e.g. with the regular expression r"\bfirefox\b". This would match
"firefox", "/usr/bin/firefox", "firefox-bin", etc, but not e.g.
"kill_firefox", e.g.:

lines = [(int(pid), cmd) for pid, cmd in lines if re.search(r'\bfirefox\b', cmd)]

That's about as good as you can get without using non-portable mechanisms
such as /proc/*/exe.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to import subprocess into my 'subprocess.py' file hiral Python 2 05-05-2010 12:56 PM
Re: Getting subprocess.call() output into a string? Nick Stinemates Python 0 04-18-2008 07:10 PM
Re: Getting subprocess.call() output into a string? David Python 0 04-15-2008 08:55 PM
[Subprocess/Windows] subprocess module under Windows 98 Andreas Jung Python 2 11-02-2005 05:41 PM
More American Graffiti: Properly Framed, Properly Scored? Scot Gardner DVD Video 0 09-02-2003 02:28 AM



Advertisments