5

Could anybody please help me to get only the second column as output of ps aux command (i.e, only the PID column).

# script to print the processid
import os
import commands

out = commands.getoutput('ps aux')  # to get the process listing in out
# print out
# print out[2]  # print only second column from out
print out[:2]

Output of print out statement

    USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
    root         1  0.0  0.0   5728  1068 ?        Ss   Oct13   0:07 /sbin/init
    root         2  0.0  0.0      0     0 ?        S<   Oct13   0:00 [kthreadd]
    root         3  0.0  0.0      0     0 ?        S<   Oct13   0:00 [migration/0]
    root         4  0.0  0.0      0     0 ?        S<   Oct13   0:11 [ksoftirqd/0]
    root         5  0.0  0.0      0     0 ?        S<   Oct13   0:00 [watchdog/0]
    root         6  0.0  0.0      0     0 ?        S<   Oct13   0:00 [migration/1]

Thanks in advance

9
  • This is actually very easy using AWK if you have access to it. ps aux | awk {'print $1'} Commented Oct 20, 2011 at 4:54
  • 2
    @RussellDias you mean ps aux | awk '{print $2}' ($0 is full line, so $1 is the first column, not second...) Commented Oct 20, 2011 at 5:21
  • @Tom: Doh! I had $2 but changed it to $1 for some odd reason (I think I tested it on cygwin which does not output USER) :( eh can't update it now Commented Oct 20, 2011 at 5:24
  • @ANV you probably want to use out = subprocess.check_output('ps aux') too. The commands module is deprecated. Commented Oct 20, 2011 at 5:27
  • @Tom: Thanks Tom. I tried but got the following errors Commented Oct 20, 2011 at 6:22

3 Answers 3

3

Use split() and splitlines() (to convert the string into a list of lines, and the list of lines into a list of columns that you can then index as needed):

>>> for line in out.splitlines():
...     fields = line.split()
...     if len(fields) >= 2:
...         print fields[1]


PID
1
2
3
4
5
6
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer. My actual need is to print the last column which is the command column. If we try the method you suggested then we get only the portion of the command column. eg:- the command column has got line as /bin/bash /bin/test.txt but it only prints /bin/bash. i wanted actually the whole command column. any suggestions would be helpful.Once again thank you for your help.
That is simple enough. Either print line[65:] or fields[10:].
Thanks exactly that's what i am looking for. If you don't mind, could you please let me know how you got the count(ie 65). Regarding fields i understood its the 10 value in the list field.
line is a string, so line[65:] is everything in line from character number 65 onwards. I presume @RaymondHettinger found that out by counting them or cutting and pasting and using len("...")...
3

As mentioned in the comments this is very straightforward to do using awk:

ps aux | awk {'print $2'}

However, here is also a python solution using a list comprehension, which gives you a list of PID's:

>>> [col.split()[1] for col in out.splitlines()]
['PID', '1', '2', '3', '4', '5', '6']

Comments

0
import subprocess

out = subprocess.check_output(['ps', 'aux']).decode('utf-8') # run command and decode output

lines = out.strip().split('\n') # split in lines

for line in lines[1:]:
    columns = line.split()
    if len(columns) > 1:
        print(columns[0])  # based on the columns[key] you can extract the specific or convert to list

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.