Published on

Exploring the ps Linux command

Authors
  • avatar
    Name
    Carter Speerschneider
    Twitter

Command Overview

The ps command is a UNIX command that displays information on all the processes as a formattable table, with records representing individual processes. Importantly, ps only returns a snapshot and not a real-time feed of process information—which has it's drawbacks.

Nonetheless, ps is a very useful command for just getting a quick-and-dirty summary of all the processes on your systems, and if configured correctly (as we are about to see), it can be a very insightful tool.

Interesting Options

There are several ways to write the flags for ps which might make it a bit confusing to beginners. I stick with unix-style (single-dashed) options to avoid confusion, but as long as you're consistent, it doesn't matter which you choose.

The first command you are likely to see shows all the running processes:

ps -ef

-e show every process -f show all standard columns in the output table

Although this is a good start, one way we can enhance this is by replacing a standard format -f with the a user-orientated output, -u.

ps -eu

You'll now see some columns that tell you information about the system resource utilization: %CPU, %MEM. You'll also notice the STAT column which tells you the state of the process. This is a very useful concept to know as this gives you a glimpse into how your scheduler is teating a process at the time of the ps call.

Building your own layout

Sometimes we don't want to use a prefinded layout; we might rather prefer to have more control over which columns are displayed.

To do this, we can use the -o option that takes a comma-delimited list of format specifiers for every column to include in the process table output.

ps -e -o user,state,pid,comm

This is very basic instance of what you can do with this option. The acceptable values are endless but for basic output, these may do.

Here is an example output on my machine:

USER     S     PID COMMAND
root     S       1 systemd
root     S       2 init-systemd(De
root     S       6 init
root     S      36 systemd-journal
root     S      75 systemd-udevd
root     S     142 cron
message+ S     143 dbus-daemon
root     S     147 systemd-logind
root     S     180 agetty
root     S     181 agetty
root     S     187 SessionLeader
root     S     188 Relay(189)
carterd+ S     189 bash
root     S     190 login
carterd+ S     200 systemd
carterd+ S     202 (sd-pam)
carterd+ S     230 bash
carterd+ R     448 ps

If you are particularly fond of a certain output, feel free to make an alias in your .bash_aliases file or similar to help you encapsulate process summarization into a single command.

Summary

The ps command is a simple but powerful tool that can be used to see various process information such as process IDs (PIDs), resource utilization, and even thread count (thcount column). While these are specialized applications for system summarization on Linux, knowing where it all begins helps you understand not only your own system but also the applications that utilize commands like ps.