Processes and jobs

Linux commands: ps

Learn how the Linux ps command lists running processes, how ps ax shows them all, and how to read columns like PID and STAT or filter the output with grep.

Your computer is running tons of processes, all the time. The ps command lets you see them.

With no options you get the processes started from your current terminal session:

Terminal output showing ps command results with PID, TTY, TIME and CMD columns displaying fish shell processes and hugo serve

Here I have a few fish shell instances, mostly opened by VS Code inside the editor, and an instance of Hugo running the development preview of a site.

Those are only the processes tied to my user and my terminal. To list all processes we need to pass some options to ps.

The one I use most is ps ax:

Terminal showing ps ax output with system processes including launchd, syslogd, and various system daemons with their PIDs and status

The a option also lists other users’ processes, not just our own. x shows processes not linked to any terminal, like the system daemons that start at boot.

As you can see, the longer commands get cut. Use ps axww to wrap the command on new lines instead of cutting it:

Terminal output of ps axww showing full command paths that wrap to new lines instead of being truncated

We need to write w 2 times to get this behavior, it’s not a typo.

That list is long. You can search for a specific process by piping it into grep:

ps axww | grep "VS Code"

Terminal showing grep filtering ps output to display only Visual Studio Code processes with their long command arguments

Now let’s look at the columns ps returns.

The first is PID, the process ID. This is the number you need to reference the process in another command, for example to kill it.

Then we have TT, the terminal the process is attached to.

STAT tells us the state of the process:

I a process that is idle (sleeping for longer than about 20 seconds) R a runnable process S a process that is sleeping for less than about 20 seconds T a stopped process U a process in uninterruptible wait Z a dead process (a zombie)

If you see more than one letter, the second one adds further information, which can get very technical.

The common ones: + means the process is in the foreground in its terminal. s means the process is a session leader.

TIME is the CPU time the process has consumed so far, not how long ago it started. A server that sat idle for a week shows a tiny TIME.

Finally, CMD is the command that started the process, with its arguments.

This command works on Linux, macOS, WSL, and anywhere you have a UNIX environment

Lesson completed