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
AWKif you have access to it.ps aux | awk {'print $1'}ps aux | awk '{print $2}'($0 is full line, so $1 is the first column, not second...)$2but changed it to$1for some odd reason (I think I tested it on cygwin which does not outputUSER) :( eh can't update it nowout = subprocess.check_output('ps aux')too. The commands module is deprecated.