The ps command on Linux is very useful for listing running processes. It is possible to list only certain running processes. And also certain columns of text from the output. This could be very useful for a script.
This example below shows how to only print the PID, the percentage of CPU and the command as well as parameters.
4.4 Thu Mar 19 jason@Yog-Sothoth 0: $ ps xu | awk '{print$2, $10, $11, $12 }' PID TIME COMMAND 452 0:00 /lib/systemd/systemd --user 470 0:00 (sd-pam) 578 0:00 -bash 3805 0:00 ps xu 3806 0:00 awk {print$2, 7754 0:24 /usr/lib/firefox/firefox -contentproc 9227 0:00 /usr/bin/gnome-keyring-daemon --daemonize 9231 0:00 /usr/lib/gdm3/gdm-x-session --run-script 9239 0:00 /usr/bin/dbus-daemon --session 9246 0:00 cinnamon-session --session 10166 0:02 /usr/lib/gnome-terminal/gnome-terminal-server 10173 0:00 /bin/bash 11084 0:00 /usr/bin/ssh-agent /usr/bin/im-launch 11096 0:00 /usr/lib/at-spi2-core/at-spi-bus-launcher 11101 0:00 /usr/bin/dbus-daemon --config-file=/usr/share/defaults/at-spi2/accessibility.conf 11103 0:00 /usr/lib/at-spi2-core/at-spi2-registryd --use-gnome-session 11115 0:00 /usr/lib/x86_64-linux-gnu/cinnamon-settings-daemon/csd-sound 11121 0:00 /usr/lib/x86_64-linux-gnu/cinnamon-settings-daemon/csd-a11y-keyboard 11122 0:00 /usr/lib/x86_64-linux-gnu/cinnamon-settings-daemon/csd-keyboard 11124 0:00 /usr/lib/x86_64-linux-gnu/cinnamon-settings-daemon/csd-a11y-settings 11126 0:00 /usr/lib/x86_64-linux-gnu/cinnamon-settings-daemon/csd-print-notifications 11137 0:00 /usr/lib/x86_64-linux-gnu/cinnamon-settings-daemon/csd-clipboard 11138 0:00 /usr/lib/x86_64-linux-gnu/cinnamon-settings-daemon/csd-xsettings 11139 0:00 /usr/lib/x86_64-linux-gnu/cinnamon-settings-daemon/csd-screensaver-proxy 11140 0:00 /usr/lib/x86_64-linux-gnu/cinnamon-settings-daemon/csd-orientation 11141 0:00 /usr/lib/x86_64-linux-gnu/cinnamon-settings-daemon/csd-media-keys 11158 0:19 /usr/bin/pulseaudio --start |
This example shows how to properly filter in awk for a certain username.
4.4 Thu Mar 19 jason@Yog-Sothoth 0: $ ps xu | awk '$1 ~ /jason/ {print $1, $7, $10, $11, $12 }' jason ? 0:00 /lib/systemd/systemd --user jason ? 0:00 (sd-pam) jason tty2 0:00 -bash jason tty3 0:34 /usr/lib/firefox/firefox -contentproc jason ? 0:00 /usr/bin/gnome-keyring-daemon --daemonize jason tty3 0:00 /usr/lib/gdm3/gdm-x-session --run-script jason ? 0:00 /usr/bin/dbus-daemon --session jason tty3 0:00 cinnamon-session --session jason ? 0:04 /usr/lib/gnome-terminal/gnome-terminal-server jason pts/0 0:00 /bin/bash jason ? 0:00 /usr/bin/ssh-agent /usr/bin/im-launch jason ? 0:00 /usr/lib/at-spi2-core/at-spi-bus-launcher jason ? 0:00 /usr/bin/dbus-daemon --config-file=/usr/share/defaults/at-spi2/accessibility.conf jason ? 0:00 /usr/lib/at-spi2-core/at-spi2-registryd --use-gnome-session jason tty3 0:00 /usr/lib/x86_64-linux-gnu/cinnamon-settings-daemon/csd-sound jason tty3 0:00 /usr/lib/x86_64-linux-gnu/cinnamon-settings-daemon/csd-a11y-keyboard jason tty3 0:00 /usr/lib/x86_64-linux-gnu/cinnamon-settings-daemon/csd-keyboard jason tty3 0:00 /usr/lib/x86_64-linux-gnu/cinnamon-settings-daemon/csd-a11y-settings jason tty3 0:00 /usr/lib/x86_64-linux-gnu/cinnamon-settings-daemon/csd-print-notifications jason tty3 0:00 /usr/lib/x86_64-linux-gnu/cinnamon-settings-daemon/csd-clipboard jason tty3 0:00 /usr/lib/x86_64-linux-gnu/cinnamon-settings-daemon/csd-xsettings jason tty3 0:00 /usr/lib/x86_64-linux-gnu/cinnamon-settings-daemon/csd-screensaver-proxy jason tty3 0:00 /usr/lib/x86_64-linux-gnu/cinnamon-settings-daemon/csd-orientation jason tty3 0:00 /usr/lib/x86_64-linux-gnu/cinnamon-settings-daemon/csd-media-keys |
This section of the awk one-liner looks for the string “jason” in column number 1.
$1 ~ /jason/ |
Another version of the same command.
4.4 Thu Mar 19 jason@Yog-Sothoth 0: $ ps aux | awk '$1 ~ /jason/ {print $1, $7, $10, $11, $12 }' jason ? 0:00 /lib/systemd/systemd --user jason ? 0:00 (sd-pam) jason tty2 0:00 -bash jason pts/0 0:00 ps aux jason pts/0 0:00 awk $1 jason tty3 0:38 /usr/lib/firefox/firefox -contentproc jason ? 0:00 /usr/bin/gnome-keyring-daemon --daemonize jason tty3 0:00 /usr/lib/gdm3/gdm-x-session --run-script jason ? 0:00 /usr/bin/dbus-daemon --session jason tty3 0:00 cinnamon-session --session jason ? 0:05 /usr/lib/gnome-terminal/gnome-terminal-server jason pts/0 0:01 /bin/bash jason ? 0:00 /usr/bin/ssh-agent /usr/bin/im-launch jason ? 0:00 /usr/lib/at-spi2-core/at-spi-bus-launcher jason ? 0:00 /usr/bin/dbus-daemon --config-file=/usr/share/defaults/at-spi2/accessibility.conf jason ? 0:00 /usr/lib/at-spi2-core/at-spi2-registryd --use-gnome-session jason tty3 0:00 /usr/lib/x86_64-linux-gnu/cinnamon-settings-daemon/csd-sound jason tty3 0:00 /usr/lib/x86_64-linux-gnu/cinnamon-settings-daemon/csd-a11y-keyboard jason tty3 0:00 /usr/lib/x86_64-linux-gnu/cinnamon-settings-daemon/csd-keyboard jason tty3 0:00 /usr/lib/x86_64-linux-gnu/cinnamon-settings-daemon/csd-a11y-settings jason tty3 0:00 /usr/lib/x86_64-linux-gnu/cinnamon-settings-daemon/csd-print-notifications jason tty3 0:00 /usr/lib/x86_64-linux-gnu/cinnamon-settings-daemon/csd-clipboard jason tty3 0:00 /usr/lib/x86_64-linux-gnu/cinnamon-settings-daemon/csd-xsettings jason tty3 0:00 /usr/lib/x86_64-linux-gnu/cinnamon-settings-daemon/csd-screensaver-proxy jason tty3 0:00 /usr/lib/x86_64-linux-gnu/cinnamon-settings-daemon/csd-orientation jason tty3 0:00 /usr/lib/x86_64-linux-gnu/cinnamon-settings-daemon/csd-media-keys |
Count the number of running processes.
4.4 Thu Mar 19 jason@Yog-Sothoth 0: $ ps aux | grep -c "^" 291 |
The proper way to filter the ps listing by username.
4.4 Thu Mar 19 jason@Yog-Sothoth 0: $ ps -u jason PID TTY TIME CMD 452 ? 00:00:00 systemd 470 ? 00:00:00 (sd-pam) 578 tty2 00:00:00 bash 7754 tty3 00:01:01 RDD Process 9227 ? 00:00:00 gnome-keyring-d 9231 tty3 00:00:00 gdm-x-session 9239 ? 00:00:00 dbus-daemon 9246 tty3 00:00:00 cinnamon-sessio 10166 ? 00:00:11 gnome-terminal- 10173 pts/0 00:00:02 bash 11084 ? 00:00:00 ssh-agent 11096 ? 00:00:00 at-spi-bus-laun 11101 ? 00:00:00 dbus-daemon 11103 ? 00:00:00 at-spi2-registr 11115 tty3 00:00:00 csd-sound 11121 tty3 00:00:00 csd-a11y-keyboa 11122 tty3 00:00:00 csd-keyboard 11124 tty3 00:00:00 csd-a11y-settin 11126 tty3 00:00:00 csd-print-notif 11137 tty3 00:00:00 csd-clipboard 11138 tty3 00:00:00 csd-xsettings 11139 tty3 00:00:00 csd-screensaver 11140 tty3 00:00:00 csd-orientation 11141 tty3 00:00:00 csd-media-keys |
This is how to filter the ps output and only show certain columns. This is a very good way to do it.
4.4 Thu Mar 19 jason@Yog-Sothoth 0: $ ps axo user,tty,pid,pcpu,comm USER TT PID %CPU COMMAND root ? 1 0.3 systemd root ? 2 0.0 kthreadd root ? 4 0.0 kworker/0:0H root ? 6 0.0 mm_percpu_wq root ? 7 0.0 ksoftirqd/0 root ? 8 0.0 rcu_sched root ? 9 0.0 rcu_bh root ? 10 0.0 migration/0 root ? 11 0.0 watchdog/0 root ? 12 0.0 cpuhp/0 root ? 13 0.0 cpuhp/1 root ? 14 0.0 watchdog/1 root ? 15 0.0 migration/1 root ? 16 0.0 ksoftirqd/1 root ? 18 0.0 kworker/1:0H root ? 19 0.0 cpuhp/2 root ? 20 0.0 watchdog/2 root ? 21 0.0 migration/2 root ? 22 0.0 ksoftirqd/2 root ? 24 0.0 kworker/2:0H root ? 25 0.0 cpuhp/3 root ? 26 0.0 watchdog/3 root ? 27 0.0 migration/3 root ? 28 0.0 ksoftirqd/3 root ? 30 0.0 kworker/3:0H |
There are many columns of text that can be shown with this one-liner. Look up the ps manual page for more.