When you are working, sometimes your programs suddenly freeze. Other times, the programs are still running but with a high processor or memory consumption. There is a way to fix this in Linux using the ps
(Process Status) command. Here we show you how to use the ps
command in Linux to list the currently running processes and their PIDs. You can then find and kill the processes consuming your resources.
Also read: The Beginner’s Guide to pstree Command on Linux
Using the “ps” command
The ps
command can be used on its own. It will output four columns of information:
- PID: the unique identifier of the process defined by the system. It’s the value that we use to stop a process.
- TTY: terminal from which the process was started.
- TIME: the total amount of CPU time used by the process.
- CMD: command that generates the process
Note that when you use the command without any options, it doesn’t show you much information. Here are a few ways to make it more useful.
1. List the process of all users
When some programs are installed, they sometimes also create some additional users to run the process. To list the processes of the users, use the -e
option:
ps -e
and its output:
PID TTY TIME CMD 1 ? 00:00:02 systemd 2 ? 00:00:00 kthreadd 3 ? 00:00:00 kworker/0:0 4 ? 00:00:00 kworker/0:0H 5 ? 00:00:00 kworker/u256:0 6 ? 00:00:00 mm_percpu_wq
2. List process with more information
It’s possible to have more information when you list the running process. To do this, you can use the ef
option.
ps -ef
and its output:
UID PID PPID C STIME TTY TIME CMD root 1 0 0 21:34 ? 00:00:03 /sbin/init maybe-ubiquity root 2 0 0 21:34 ? 00:00:00 [kthreadd] root 3 2 0 21:34 ? 00:00:00 [kworker/0:0] root 4 2 0 21:34 ? 00:00:00 [kworker/0:0H] root 6 2 0 21:34 ? 00:00:00 [mm_percpu_wq] root 7 2 0 21:34 ? 00:00:00 [ksoftirqd/0]
3. Filter the Process by Process ID
If you know the process ID of the running process you want to show, you can filter for it specifically with the -p
flag. This can take multiple PIDs as arguments, separated by a single comma and no space.
ps -ef -p 1234,5678,9012
4. List the processes owned by a user
You can also list the processes that are owned by a user with the u
option followed by the name of the user:
ps -u userName
and its output:
PID TTY TIME CMD 2832 ? 00:00:00 systemd 2842 ? 00:00:00 (sd-pam) 3043 ? 00:00:00 sshd 3044 pts/1 00:00:00 bash 18396 pts/1 00:00:00 ps
5. List the actives processes
It’s possible to list all the processes that are active by using the ax
option:
ps -ax
and its output:
PID TTY STAT TIME COMMAND 1 ? Ss 0:02 /sbin/init maybe-ubiquity 2 ? S 0:00 [kthreadd] 3 ? I 0:00 [kworker/0:0] 4 ? I< 0:00 [kworker/0:0H] 6 ? I< 0:00 [mm_percpu_wq] 7 ? S 0:00 [ksoftirqd/0]
6. List the active processes with the users
It’s possible to list all the active processes with the users when you add the -aux
flag:
ps -aux
and its output:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.2 78132 9188 ? Ss 21:34 0:02 /sbin/init maybe-ubiquity root 2 0.0 0.0 0 0 ? S 21:34 0:00 [kthreadd] root 3 0.0 0.0 0 0 ? I 21:34 0:00 [kworker/0:0] root 4 0.0 0.0 0 0 ? I< 21:34 0:00 [kworker/0:0H] root 6 0.0 0.0 0 0 ? I< 21:34 0:00 [mm_percpu_wq] root 7 0.0 0.0 0 0 ? S 21:34 0:00 [ksoftirqd/0] root 8 0.0 0.0 0 0 ? I 21:34 0:00 [rcu_sched] root 9 0.0 0.0 0 0 ? I 21:34 0:00 [rcu_bh] root 10 0.0 0.0 0 0 ? S 21:34 0:00 [migration/0]
7. Filter the process by the name of a program
It’s possible to retrieve the information about a specific program that is running by applying a filter on the ps
result:
ps -aux | grep docker
and its output:
root 1508 0.0 2.2 1518156 90868 ? Ssl 21:34 0:03 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock userkub+ 18429 0.0 0.0 13144 1108 pts/1 S+ 23:57 0:00 grep --color=auto docker
Alternatively, you can also use the C
option to filter the process by its name:
ps -C name
8. Display Specific Columns
In addition to the four default columns, you can get ps
to display an additional column of information. For example:
ps -e -o pid,uname,pcpu,pmem,comm
The -o
flag sets specific output display options for the ps
command’s results. See a full list of standard display options for ps.
9. Display Results in Hierarchical Tree Style
ps -e --forest
This uses ASCII art to create a tree-style structure for displaying processes. Shows forked and children processes as descendants of the appropriate parent processes, sorting to match. To hide the “branches” of the tree, use -H
in place of --forest
.
10. Display Process Threads
ps -p 4041 -L
The -L
flag toggles on a threaded display for any functionality of ps. It’s most useful when tracking down the threads of a specific process.
11. Show All Root Processes
ps -f -U root -u root
Execute a search for all processes running with real and effective root identifications. This shows them in the full-length format, thanks to the -f
flag. You can combine it with the -o
flag to customize output.
Use the kill command to stop a process
Once you have located the misbehaving process, you can use the kill command to kill a process that is running. The command sends a signal to a process that terminates it. When your programs are frozen, most of the time you will need to forcefully kill them with the -9
option.
The output of ps is an instant view. Unlike htop
, It does not update itself dynamically. This means you might have to run it multiple times to get a clear picture of which process is misbehaving. To get an up-to-date view of the processes, you can try some other commands for the Linux system.
Our latest tutorials delivered straight to your inbox