Listing and monitoring processes

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.

Listing processes

To know a list of processes running in the system, we can use the ps command.

Syntax

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.

Simple process selection

The following table shows the multiple options that can be clubbed together and used to get a better selection of results:

Option

Description

-A, -e

Selects all processes

-N

Selects all processes that don't fulfill a condition—that is, negate selection

T

Selects the processes associated with the current terminal

r

Restricts selection to only running processes

x

Selects processes that have no controlling terminal such as daemons launched during booting

a

Selects the processes on a terminal including all users

Process selection by list

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

-C cmdlist

Selects the process by its name. The list of names for selection is provided in cmdlist.

-g grplist

Selects the process by an effective group name provided in the list of the grplist arguments.

-G grplist

Selects the process by a real group name provided in the list of the grplist arguments.

-p pidlist

Selects the process by its PID mentioned in pidlist.

-t ttylist

Selects the process by a terminal mentioned in ttylist.

-U userlist

Selects the process by a real user ID or name mentioned in userlist.

-u userlist

Selects the process by an effective user ID or name mentioned in userlist.

Output format control

The following options are used to choose how to display the ps command output:

Option

Description

-j

Shows the job format.

-f

This is used for a full format listing. It also prints the argument passed to the command.

u

Displays user-oriented format.

-l

Displays long format.

v

Displays the virtual memory format.

Listing all processes with details

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
Listing all processes with details

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.

Listing all processes run by a user

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:

Listing all processes run by a user

We see that all processes are running as the user root. The rest of the users' processes have been filtered out.

Processes running in the current terminal

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:

Processes running in the current terminal

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.

Listing processes by a command name

We can also know process details by its name using the -C option followed by the command name. Multiple command names can be separated by a comma (,):

$ ps u -C firefox,bash

The following output is obtained:

Listing processes by a command name

Tree format display of processes

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
Tree format display of processes

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
Tree format display of processes

Use the pstree command with the -u option to see when the UID of the process and parent differs:

$ pstree -pu 1627
Tree format display of processes

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.

Monitoring processes

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
Monitoring processes

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

us

% of the CPU time spent in running un-niced user processes

sy

% of the CPU time spent in kernel space—that is running kernel processes

ni

% of the CPU time running niced user processes

id

% of the time spent idle

wa

% of the time spent waiting for the I/O completion

hi

% of the time spent servicing the hardware interrupt

si

% of the time spent servicing the software interrupts

st

% 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:

Monitoring processes

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:

Monitoring processes

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:

Monitoring processes
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.54.75