Re: getting properly one subprocess output
On Nov 18, 12:25 pm, Jean-Michel Pichavant jeanmic...@sequans.com 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) -- http://mail.python.org/mailman/listinfo/python-list
Re: getting properly one subprocess output
Jean-Michel Pichavant jeanmic...@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. -- http://mail.python.org/mailman/listinfo/python-list
Re: getting properly one subprocess output
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. -- http://mail.python.org/mailman/listinfo/python-list
Re: getting properly one subprocess output
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 -- http://mail.python.org/mailman/listinfo/python-list
getting properly one subprocess output
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) -- http://mail.python.org/mailman/listinfo/python-list
Re: getting properly one subprocess output
On Nov 18, 11:25 am, Jean-Michel Pichavant jeanmic...@sequans.com 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/1bash 13755 pts/1ps -eo pid,tty,cmd ...etc... hth, Jon. -- http://mail.python.org/mailman/listinfo/python-list
Re: getting properly one subprocess output
On Nov 18, 4:14 pm, Jon Clements jon...@googlemail.com wrote: On Nov 18, 11:25 am, Jean-Michel Pichavant jeanmic...@sequans.com 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: getting properly one subprocess output
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. -- http://mail.python.org/mailman/listinfo/python-list