getting properly one subprocess output

  • Thread starter Jean-Michel Pichavant
  • Start date
J

Jean-Michel Pichavant

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)
 
J

Jon Clements

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

Jon Clements

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 ?

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

Nobody

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

Jean-Michel Pichavant

Nobody said:
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
 
B

Bas

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)
 
P

Paul Rudin

Jean-Michel Pichavant said:
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.
 
N

Nobody

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top