02. Viewing Processes ps

We can easily view processes, using ps (process status). This only displays the processes that run from the current terminal, so are limited.

$ ps
 PID TTY         TIME CMD
2696 pts/0    0:00:00 bash
2745 pts/0    0:00:00 ps

At the most basic level, there are only four columns.

PID
Process identifier. Necessary for sending signals.
TTY
Teletype. Refers to the terminal that is controlling the process. X programs and daemons don't have this, but text-mode programs do (eg. console, xterm or remote login session).
TIME
Amount of CPU time consumed by the process. The higher it is, the more resources it's taking from the computer.
CMD
The command that started the process.

Options with ps

As we'll see soon, the ps command comes with a variety of options. Depending on where these options originated, you may or may not need dashes preceding the option letter.

BSD
These come from an earlier Berkeley Software Distribution platform and require no dashes at all.
Unix98
These options are preceded by one dash, and are probably what you're most familiar with.
GNU long
As you're most likely familiar with, any GNU long options are preceded by two dashes (--).

More details with -ef

The -f option prints full format listing, meaning it includes additional parameters. The -e or -A options displays all processes (including daemon).

$ ps -ef
UID PID PPID C STIME TTY TIME CMD root 1 0 3 11:14 ? 00:00:00 /sbin/init root 2 0 0 11:14 ? 00:00:00 [kthreadd] root 3 2 0 11:14 ? 00:00:00 [ksoftirqd/0] root 4 2 0 11:14 ? 00:00:00 [kworker/0:0] root 5 2 0 11:14 ? 00:00:00 [kworker/0:0H] root 6 2 1 11:14 ? 00:00:00 [kworker/u2:0] root 7 2 0 11:14 ? 00:00:00 [rcu_sched] root 8 2 0 11:14 ? 00:00:00 [rcuos/0] root 9 2 0 11:14 ? 00:00:00 [rcu_bh]

You'll notice that some processes have a TTY of ?. This means that they have no controlling terminals. Many of these are daemon processes.

Here are some more parameters listed, adding onto the list above.

UID
User ID
PPID
Parent process identifier.
C
CPU utilization.
STIME
Time the process started.

Viewing user processes

To view all processes belonging to a user, use the -u option, followed by the username. If you want to view more than one username, separate the names with a comma.

$ ps -u JohnDoePC
# List all processes started by JohnDoePC

More details with aux

By attaching an aux option (no hyphen), we can see even more details. The a shows processes for all users, the u displays the process's owner, and the x shows processors not attached to a terminal.

Additionally, the w option shows the full command name, not just what fits on one line.

USER      PID %CPU %MEM    VSZ   RSS TTY    STAT START   TIME COMMAND
root      1    0.4  0.4    33772  2012 ?      Ss 22:53   0:01 /sbin/init
root      2    0.0  0.0        0     0 ?      S  22:53   0:00 [kthreadd]
root      3    0.0  0.0        0     0 ?      S  22:53   0:00 [ksoftirqd/0]
root      4    0.0  0.0        0     0 ?      S  22:53   0:00 [kworker/0:0]
root      5    0.0  0.0        0     0 ?      S< 22:53   0:00 [kworker/0:0H]
root      6    0.0  0.0        0     0 ?      S  22:53   0:00 [kworker/u2:0]
root      7    0.2  0.0        0     0 ?      S  22:53   0:00 [rcu_sched]
root      8    0.0  0.0        0     0 ?      S  22:53   0:00 [rcuos/0]
root      9    0.0  0.0        0     0 ?      S  22:53   0:00 [rcu_bh]

Here are some more variables that we see.

USER
User ID, or the owner of process.
%CPU
CPU usage as a percentage.
%MEM
Memory usage.
VSZ
Virtual memory size.
RSS
Resident set size - amount of physical memory (RAM) the process is using (kb).
STAT
Short for state. Reveals the current status of the process. See below for explanation.
START
Time when processor started.
TIME
Amount of CPU time process has acquired so far. Helpful for pinpointing runaway processes.

State Variables

Here are some variables that go along with the STAT column.

D
Uninterruptible sleep - waiting for I/O device, such as a drive.
L
Pages are locked into memory.
R
Running, or ready to run.
S
Sleeping, or waiting for user input.
T
Stopped
Z
Defunct, or a zombie process, aka a child process that stopped, but has not been cleaned up by parent process
<
High priority. Low niceness, meaning it takes up more resources.
+
In the foreground process group.
N
Low priority process. A nice process
l
Multi-threaded.
s
A session leader.

Inspecting current shell processes

To access the current shell's PID, use the shortcut $$. This evaluates to the current shell's PID.

$ ps u $$
USER       PID   %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
JohnDoe    13030  0.0  0.0  27112  4092 pts/8    Ss   00:39   0:00 bash

Displaying process hierarchy

If you want to see where each process spawned from, we can use the -H, -f or --forest option. You'll see that each child process branches out with tabs, signifying its origin

$ ps -AH
...
    1 ?        00:00:01 init
  299 ?        00:00:00   upstart-udev-br
  304 ?        00:00:00   systemd-udevd
  517 ?        00:00:00   upstart-socket-
  774 ?        00:00:00   upstart-file-br
  783 ?        00:00:00   rsyslogd
  792 ?        00:00:03   dbus-daemon
  805 ?        00:00:00   ModemManager
  816 ?        00:00:00   systemd-logind
  818 ?        00:00:00   bluetoothd
  850 ?        00:00:01   avahi-daemon
  852 ?        00:00:00     avahi-daemon
...

One drawback with using ps is that it's static - we cannot see the changes while they occur. For a more dynamic view of currently running processes, we can use the top command.

Aching back from coding all day?

Self-Massage Tool

Aching back from coding all day? Try Back Problems

Relieve spasms, tight muscles, trigger points and pressure points with the Body Back Buddy! This trigger point massage is designed to help you self-message any area of your body - especially those that are hard to reach. Keeping your muscles relaxes and out of contraction is importan in helping to reduce pain and prevent muscle injury.

$ Check price
29.9529.95Amazon 4.5 logo(3,443+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

Linux for Beginners

Take your Linux skills to the next level! Try Linux & UNIX

Linux for Beginners doesn't make any assumptions about your background or knowledge of Linux. You need no prior knowledge to benefit from this book. You will be guided step by step using a logical and systematic approach. As new concepts, commands, or jargon are encountered they are explained in plain language, making it easy for anyone to understand.

$ Check price
24.9924.99Amazon 4.5 logo(101+ reviews)

More Linux & UNIX resources

Ad