In a running system, we often notice that suddenly a system is responding slowly. This can be because a running application is consuming a lot of memory or a process is doing CPU-intensive work. It's hard to predict which application is causing the system to respond slower. To know the reason, it is good to know what all processes are running and also know the monitoring behavior (such as the amount of CPU or memory being consumed) of processes.
To know a list of processes running in the system, we can use the ps
command.
The syntax of the ps
command is as follows:
ps [option]
There are a lot of options to use the ps
command. The commonly used options are explained in the following table.
The following table shows the multiple options that can be clubbed together and used to get a better selection of results:
Option |
Description |
---|---|
|
Selects all processes |
|
Selects all processes that don't fulfill a condition—that is, negate selection |
|
Selects the processes associated with the current terminal |
|
Restricts selection to only running processes |
|
Selects processes that have no controlling terminal such as daemons launched during booting |
|
Selects the processes on a terminal including all users |
The following options accept a single argument in the form of a blank-separated or comma-separated list; they can be used multiple times:
Option |
Description |
---|---|
|
Selects the process by its name. The list of names for selection is provided in |
|
Selects the process by an effective group name provided in the list of the |
|
Selects the process by a real group name provided in the list of the |
|
Selects the process by its PID mentioned in |
|
Selects the process by a terminal mentioned in |
|
Selects the process by a real user ID or name mentioned in |
|
Selects the process by an effective user ID or name mentioned in |
The following options are used to choose how to display the ps
command output:
Option |
Description |
---|---|
|
Shows the job format. |
|
This is used for a full format listing. It also prints the argument passed to the command. |
|
Displays user-oriented format. |
|
Displays long format. |
|
Displays the virtual memory format. |
To know all processes on a system, the -e
option can be used. To have a more detailed output, use it with the u
option:
$ ps -e u | wc -l # Total number of processes in system 211 $ ps -e u | tail -n5 # Display only last 5 line of result
We can see from the output that all users' processes are displayed. The command that is actually displaying the output—that is, ps -e u | tail -n5—is also mentioned in the ps
output as two separate running processes.
In BSD style, use the aux
option to get the result that we get from -e u
:
$ ps aux
On a Linux-based operating system, aux as well as -e u
options will work fine.
To know which processes are being by a specific user, use the -u
option followed by the username. Multiple usernames can also be provided separated by a comma (,).
$ ps u -u root | wc -l 130 $ ps u -u root | tail -n5 # Display last 5 results
The preceding command displays the following result:
We see that all processes are running as the user root. The rest of the users' processes have been filtered out.
It is useful to know which processes are running in the current terminal. It can help in deciding whether to kill a running terminal or not. We can make a list of processes running in the current terminal using the T
or t
option.
$ ps ut
The output for the following command as follows:
We can see from the output that bash
and the ps uT
command (which we just executed to display the result) are only running processes in the current terminal.
The
pstree
command displays running processes in a tree structure, which makes it very easy to understand the parent and child relationship of processes.
Running the pstree
command with the -p
option shows processes in the tree format with its PID number as follows:
$ pstree -p
From the pstree
output, we see that the parent process of all processes is systemd
. This is started as the first process that is responsible for executing the rest of the processes. In parenthesis, the PID number of each process is mentioned. We can see that the systemd
process got PID 1 that is always fixed. On the init
based-operating system, init
will be the parent of all processes and have PID 1.
To see processes process the tree of a particular PID, we can use pstree
with the PID number as an argument:
$ pstree -p 1627 # Displays process tree of PID 1627 with PID number
Use the pstree
command with the -u
option to see when the UID of the process and parent differs:
$ pstree -pu 1627
We can see that initially, bash
is being run by the user skumari
with the PID 1627
. Further down in the tree, the sudo
command is running as a root.
It is very important to know how much memory and CPU a process is consuming while running, in order to ensure there is no leak of memory and over-CPU computation happening. There are commands such as top
, htop
, and vmstat
that can be used to monitor the memory and CPU consumed by each process. Here, we will discuss the top
command because it is preinstalled in a Linux-based operating system.
The top
command displays the dynamic real-time usage of the CPU, memory, swap, and the number of tasks currently running with their state.
Running top
without any options gives the following result:
$ top
In the top
command output, the first line tells us about the length of time since the system last booted, the number of users, and the load average.
The second line tells us about the number of tasks and their statuses—running, sleeping, stopped, and zombie.
The third line gives us the details of the CPU usage in percentage. The different CPU usages are shown in the following table:
Value |
Description |
---|---|
|
% of the CPU time spent in running un-niced user processes |
|
% of the CPU time spent in kernel space—that is running kernel processes |
|
% of the CPU time running niced user processes |
|
% of the time spent idle |
|
% of the time spent waiting for the I/O completion |
|
% of the time spent servicing the hardware interrupt |
|
% of the time spent servicing the software interrupts |
|
% of the time consumed by a virtual machine |
The fourth line tells us about the total, free, used, and buffered RAM memory usage.
The fifth line tells us about the total, free and used swap memory.
The remaining lines give the detailed information about running processes. The meaning of each column is described in the following table:
Column |
Description |
---|---|
PID |
Process ID |
USER |
Effective user name of task's owner |
PR |
Priority of task (lower the value, more is the priority) |
NI |
Nice value of task. Negative nice value means more priority and positive means lesser priority |
VIRT |
Virtual memory size used by process |
RES |
Non-swapped physical memory a process |
SHR |
Amount of shared memory available to a process |
S |
Process status – D (uninterruptible sleep ), R (Running), S(Sleeping), T (Stopped by job control signal), t (Stopped by debugger), Z (Zombie) |
%CPU |
% of CPU currently used by process |
%MEM |
% of Physical memory currently used by process |
TIME+ |
CPU Time, hundredths |
COMMAND |
Command name |
We can also reorder and modify the output when the top is running. To see help, use the ? or h key and the help window will be displayed, which contains following details:
To sort on the basis of a specific field, the easiest method is to press the f key while top
is running. A new window opens showing all the columns. The opened window looks as follows:
Use the up and down arrows to navigate and select a column. To sort on the basis of a particular field, press the s key and then press q to switch back to the top output window.
Here, we have selected NI and then pressed the s key and the q key. Now, the top
output will be sorted with nice
number. The output of the top after sorting with the column NI looks as follows:
18.116.14.111