12

I know that we can easily find the following:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND

of all the running processes by using the command:

sudo ps aux

My, question is that, is it possible to show the output of only the column under COMMAND and nothing else?

18

Use -o flag.

To list only your own processes

 ps -o command  

To list all system processes

 ps -e -o command

This is not the only way to list commands, they can either be printed with command line flags, or as executable only (which is what command option does).

From my comment bellow the answer:

command, args, and cmd all give full command. In fact man page states command and cmd are aliases for args , with the - flags included. The comm gives the name of the exacutable only. Aliases to that are ucmd and ucomm. I misread about AIX options, those can be actually specified with printf-like format, %a for args, %c for comm

Refer to man ps for more info on usage and available format options

Programmatic approach would be a bit redundant , since ps already provides us with the formatting options, but it can be done with awk, which is much useful when dealing with columnized output.

ps aux | awk '{ for(i=1;i<=NF;i++) {if ( i >= 11 ) printf $i" "}; printf "\n" }'

Note however, that this code breaks if username contains whitespace, e.g. john doe. This can be amended with adding gsub function that will elimiate your username from the ps list. However, if there is multiple users logged into system, that may be difficult to errase all of the usernames from the output. Thus, you can see that -o flag is much more preferred.

Side note, sudo is not necessary to for listing all processes with ps

  • Thank You! Are there any differences between ps -eo command & ps -eo comm? The only difference I found was in command the output was as such: [kthreadd] whereas in comm the output was as this: kthreadd. Does this signify anything? – Raphael Nov 18 '15 at 7:19
  • @Raphael according to the man page, the difference is that comm is AIX format, where as command is standard GNU style format. AIX is another *NIX like system, so that option may be used for compatability. Otherwise, no difference – Sergiy Kolodyazhnyy Nov 18 '15 at 7:24
  • @Serg, I just executed the diff <(ps -eo comm) <(ps -eo command) and there seems to be quite a rattle down there, can you clarify? – Andrew Nov 18 '15 at 7:42
  • Watch out, the awk solution breaks if an username contains spaces (here); also maybe just ps aux | awk '{ for(i=11;i<=NF;i++) {printf $i" "}; printf "\n" }'? – kos Nov 18 '15 at 7:59
  • 2
    @Andrew So, I've read the manual a little more. So command, args, and cmd all give full command. In fact man page states command and cmd are aliases for args , with the - flags included. The comm gives the name of the exacutable only. Aliases to that are ucmd and ucomm. I misread about AIX options, those can be actually specified with printf-like format, %a for args, %c for comm – Sergiy Kolodyazhnyy Nov 18 '15 at 8:08
4

A more generic way. We determine the column of the command. In this way, it does not matter which switches are used for ps.

ps ax | awk -v p='COMMAND' 'NR==1 {n=index($0, p); next} {print substr($0, n)}'

Sample output

% ps aux | awk -v p='COMMAND' 'NR==1 {n=index($0, p); next} {print substr($0, n)}'
/sbin/init splash
[kthreadd]
[ksoftirqd/0]
[kworker/0:0H]
[rcu_sched]
[rcu_bh]
[migration/0]
[watchdog/0]
[watchdog/1]
[migration/1]
[ksoftirqd/1]
[kworker/1:0H]
[watchdog/2]
[migration/2]
[ksoftirqd/2]
[kworker/2:0H]
[khelper]
[kdevtmpfs]
[netns]
[perf]
[khungtaskd]
[writeback]
[ksmd]
[khugepaged]
[crypto]
[kintegrityd]
[bioset]
[kblockd]
[ata_sff]
[md]
[devfreq_wq]
[kswapd0]
[fsnotify_mark]
[ecryptfs-kthrea]
[kthrotld]
[acpi_thermal_pm]
[scsi_eh_0]
[scsi_tmf_0]
[scsi_eh_1]
[scsi_tmf_1]
[ipv6_addrconf]
[deferwq]
[charger_manager]
[kpsmoused]
[scsi_eh_2]
[scsi_tmf_2]
[kworker/0:1H]
[jbd2/sda1-8]
[ext4-rsv-conver]
[kauditd]
[kworker/0:2]
/lib/systemd/systemd-journald
/lib/systemd/systemd-udevd
[…]

And what's about pstree?

Sample output

% pstree -alU
systemd splash
  ├─ModemManager
  │   ├─{gdbus}
  │   └─{gmain}
  ├─NetworkManager --no-daemon
  │   ├─dhclient -d -q -sf /usr/lib/NetworkManager/nm-dhcp-helper -pf /run/sendsigs.omit.d/network-manager.dhclient-eth0.pid -lf /var/lib/NetworkManager/dhclient-57cfb005-1dc6-410d-aebc-16d8d152abfd-eth0.lease -cf /var/lib/NetworkManager/dhclient-eth0.conf eth0
  │   ├─dnsmasq --no-resolv --keep-in-foreground --no-hosts --bind-interfaces --pid-file=/run/sendsigs.omit.d/network-manager.dnsmasq.pid --listen-address=127.0.1.1 --conf-file=/var/run/NetworkManager/dnsmasq.conf --cache-size=0 --proxy-dnssec --enable-dbus=org.freedesktop.NetworkManager.dnsmasq --conf-dir=/etc/NetworkManager/dnsmasq.d
  │   ├─{gdbus}
  │   └─{gmain}
  ├─VBoxClient --clipboard
  │   └─VBoxClient --clipboard
  │       └─{SHCLIP}
  ├─VBoxClient --display
  │   └─VBoxClient --display
  │       └─{VT_MONITOR}
  ├─VBoxClient --seamless
  │   └─VBoxClient --seamless
  │       └─{X11 events}
  ├─VBoxClient --draganddrop
  │   └─VBoxClient --draganddrop
  │       ├─{dndHGCM}
  │       └─{dndX11}
  ├─VBoxService --pidfile /var/run/vboxadd-service.pid
  │   ├─{automount}
  │   ├─{control}
  │   ├─{cpuhotplug}
  │   ├─{memballoon}
  │   ├─{timesync}
  │   ├─{vminfo}
  │   └─{vmstats}
  ├─VBoxService
  │   ├─{automount}
  │   ├─{control}
  │   ├─{cpuhotplug}
  │   ├─{timesync}
  │   ├─{vminfo}
  │   └─{vmstats}
  ├─accounts-daemon
  │   ├─{gdbus}
  │   └─{gmain}
  ├─agetty --noclear tty1 linux
  ├─apache2 -k start
  │   ├─apache2 -k start
  │   ├─apache2 -k start
  │   ├─apache2 -k start
  │   ├─apache2 -k start
  │   └─apache2 -k start
  ├─at-spi-bus-laun
  │   ├─dbus-daemon --config-file=/etc/at-spi2/accessibility.conf --nofork --print-address 3
  │   ├─{dconf worker}
  │   ├─{gdbus}
  │   └─{gmain}
  ├─at-spi2-registr --use-gnome-session
  │   ├─{gdbus}
  │   └─{gmain}
  ├─atd -f
  ├─avahi-daemon
  │   └─avahi-daemon
  ├─cgmanager -m name=systemd
  ├─colord
  │   ├─{gdbus}
  │   └─{gmain}
  ├─console-kit-dae --no-daemon
  │   ├─62*[{console-kit-dae}]
  │   ├─{gdbus}
  │   └─{gmain}
  ├─cron -f
  ├─cups-browsed
  │   ├─{gdbus}
  │   └─{gmain}
  ├─cupsd -l
  │   └─dbus dbus:// 
  ├─dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
  ├─dnsmasq -u lxc-dnsmasq --strict-order --bind-interfaces --pid-file=/run/lxc/dnsmasq.pid --listen-address 10.0.3.1 --dhcp-range 10.0.3.2,10.0.3.254 --dhcp-lease-max=253 --dhcp-no-override --except-interface=lo --interface=lxcbr0 --dhcp-leasefile=/var/lib/misc/dnsmasq.lxcbr0.leases --dhcp-authoritative
  ├─gdm
  │   ├─gdm-session-wor
  │   │   ├─gdm-x-session /usr/bin/gnome-session --autostart /usr/share/gdm/greeter/autostart
  │   │   │   ├─Xorg vt7 -displayfd 3 -auth /run/user/120/gdm/Xauthority -nolisten tcp -background none -noreset -keeptty -verbose 3
  │   │   │   │   ├─{llvmpipe-0}
  │   │   │   │   ├─{llvmpipe-1}
  │   │   │   │   └─{llvmpipe-2}
  │   │   │   ├─dbus-daemon --print-address 4 --session
  │   │   │   ├─gnome-session-b --autostart /usr/share/gdm/greeter/autostart
  │   │   │   │   ├─gnome-settings-
  │   │   │   │   │   ├─{dconf worker}
  │   │   │   │   │   ├─{gdbus}
  │   │   │   │   │   ├─{gmain}
  │   │   │   │   │   └─{pool}
  │   │   │   │   ├─gnome-shell --mode=gdm
  │   │   │   │   │   ├─ibus-daemon --xim --panel disable
  │   │   │   │   │   │   ├─ibus-dconf
  │   │   │   │   │   │   │   ├─{dconf worker}
  │   │   │   │   │   │   │   ├─{gdbus}
  │   │   │   │   │   │   │   └─{gmain}
  │   │   │   │   │   │   ├─ibus-engine-sim
  │   │   │   │   │   │   │   ├─{gdbus}
  │   │   │   │   │   │   │   └─{gmain}
  │   │   │   │   │   │   ├─{gdbus}
  │   │   │   │   │   │   └─{gmain}
  │   │   │   │   │   ├─{JS GC Helper}
  │   │   │   │   │   ├─{JS Sour~ Thread}
  │   │   │   │   │   ├─{Sync}
  │   │   │   │   │   ├─{dconf worker}
  │   │   │   │   │   ├─{gdbus}
  │   │   │   │   │   ├─{gmain}
  │   │   │   │   │   ├─{llvmpipe-0}
  │   │   │   │   │   ├─{llvmpipe-1}
  │   │   │   │   │   ├─{llvmpipe-2}
  │   │   │   │   │   └─{threaded-ml}
  │   │   │   │   ├─{dconf worker}
  │   │   │   │   ├─{gdbus}
  │   │   │   │   └─{gmain}
  │   │   │   ├─{gdbus}
  │   │   │   └─{gmain}
  │   │   ├─{gdbus}
  │   │   └─{gmain}

[…]
  • More generic way added. – A.B. Nov 18 '15 at 9:11
  • Bravo, A.B. ! You solved what me and @kos were trying to figure out - eliminating username – Sergiy Kolodyazhnyy Nov 18 '15 at 9:31
1

I found a bashy way to achieve this from here

sudo ps aux | rev | cut -d ' ' -f 1 | rev

rev reverses the output of ps and then cut can help you out delimiting the first field(which is the last!) and then reverse it again...

worked for me.

  • 2
    Processes with a space in their name won't be listed correctly by this method: ps aux itself will be listed as aux. – kos Nov 18 '15 at 7:07
  • 1
    sudo isn't necessary for ps and the command crops a lot of the commands, so it's not very accurate, if you would like to work with colums , use awk it's much better for that task. In fact, the exact command would be ps aux | awk '{ for(i=1;i<=NF;i++) {if ( i >= 11 ) printf $i" "}; printf "\n" }' – Sergiy Kolodyazhnyy Nov 18 '15 at 7:09
  • @kos yeah, I just realised it, thanks for pointing it out, Serg's awk should work it. – Andrew Nov 18 '15 at 7:20
  • @Serg +1, OP had requested sudo ps and I thought awk would be too complicated for this task and came out with cut. Anyways, shouldn't you update your answer with that code so the OP can accept it? – Andrew Nov 18 '15 at 7:21
  • It's better, but technically usernames on Linux can contain spaces, which could lead that command to mess up as well. ps it's just not blindly parsable. I think the only safe method here is ps -e -o. – kos Nov 18 '15 at 7:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.