Managing Linux processes

Table of contents

Control processes

Managing processes in Linux using the pscommand can be a powerful way to view and control the processes running on your system. The ps command allows you to see a detailed list of all the processes currently running on your system, including their process IDs (PIDs), the user that started them, and their current status. You can also use various options and filters with the ps command to display information about specific processes, or to display information in a particular format.

Default behavior of ps command:

  • The default behavior of the "ps" command is to display only the processes that are associated with the current user and are active on the current terminal.
  • The standard output of the "ps" command includes the process ID (PID) of the programs, the terminal (TTY) on which they are running and the amount of CPU time the process has consumed.

Command line parameters

  • Parameters in the Unix style, denoted by a preceding hyphen
  • Parameters in the BSD style, denoted by no preceding hyphen
  • Long parameters in the GNU style, denoted by a double hyphen.

The Unix-style parameters originated with the original ps command that run on the AT&T Unix systems invented by Bell Labs. Table show these parameters:

Parameter Description
-A Shows all processes
-N Show the opposite of the specified parameters
-a Shows all processes except session headers and processes without a terminal
-d Shows all processes except session headers
-e Shows all processes
-C cmslist Shows processes contained in the list cmdlist
-G grplist Shows processes with a group ID listed in grplist
-U userlist Shows processes owned by a userid listed in userlist
-g grplist Shows processes by session or by groupid contained in grplist
-p pidlist Shows processes with PIDs in the list pidList
-s sesslist Shows processes with a session ID in the list sesslist
-t ttylist Shows processes with terminal ID in the list ttylist
-u userlist Shows processes by effective userid in the list userlist
-F Uses extra full output
-O format Displays specific columns in the list format along with default columns
-M Displays security information about the process
-f Displays a full format listing
-j Shows job information
-l Displays a long listing
-o format Displays only specifc columns listed in format
-y Prevents display of process flags
-Z Displays the security context information
-H Displays processes in a hierarchical format (showing parent processes)
-n namelist Defines the values to display in the WCHAN column
-w Uses wide output format, for unlimited width displays
-L Shows process threads
-V Displays the version of ps

See all processes

We can see all processes running on the system by using -e and -f parameters. The output shows some useful information:

Column Description
UID The user who launched the process
PID The unique identification number of the process
PPID The process ID of the parent process if the process is launched by another one
C Processor utilization over the lifetime of the process
STIME The time when the process started as per the system time
TTY The terminal device where the process was launched
TIME The total amount of CPU time that the process has consumed so far
CMD The name of the program that was started to initiate the process

When using the BSD-style parameters, the ps command automatically changes the output to simulate the BSD format. An example of this is using the "l" parameter:

ps l

The BSD-style parameters produce a more detailed state code for processes, shown in the "STAT" column. The two-character code provides more specific information about the current state of the process compared to the single-character Unix-style output.

Here is an example of the two-character state code:

Code Description
< The process is running at high priority.
N The process is running at low priority.
L The process has pages locked in memory.
s The process is a session header.
l The process is multi-threaded.
+ The process is running in the foreground.

Additionally, the BSD-style parameters also produce the following additional columns:

Column Description
VSZ The size in kilobytes of the process in memory
RSS The physical memory used by the process that isn't swapped out

GNU long parameters

The developers of GNU added some new options to the ps command, by introducing the GNU long parameters, some of which are similar to existing Unix- or BSD-style parameters, while others bring new capabilities. The table below provides a list of the available GNU long parameters.

Parameter Description
--deselect Shows all processes except those listed in the command line
--Group grplist Shows processes whose group ID is listed in grplist
--User userlist Shows processes whose user ID is listed in userlist
--group grplist Shows processes whose effective group ID is listed in grplist
--pid pidlist Shows processes whose process ID is listed in pidlist
--ppid pidlist Shows processes whose parent process ID is listed in pidlist
--sid sidlist Shows processes whose session ID is listed in sidlist
--tty ttylist Shows processes whose terminal device ID is listed in ttylist
--user userlist Shows processes whose effective user ID is listed in userlist
--format format Displays only columns specified in the format
--context Displays additional security information
--cols n Sets screen width to n columns
--columns n Sets screen width to n columns
--cumulative Includes stopped child process information
--forest Displays processes in a hierarchical listing showing parent processes
--headers Repeat column headers on each page of output
--no-headers Prevents display of column headers
--lines n Set the screen height to n lines
--rows n Sets the screen height to n roews
--sort order Defines the column(s) to use for sorting the output
--width n Sets the screen width to n columns

Real Time process monitoring

The ps command is a useful tool for viewing information about processes running on a system, but it has the limitation of only displaying information at a specific point in time. top command solves this problem by providing real-time process monitoring by updating the information continuously.

The top command output is divided into several sections. The first section provides general system information such as the current time, uptime, number of logged in users, and system load averages. The load averages (1-minute, 5-minute, and 15-minute) indicate the level of activity on the system. High load averages can indicate that the system is under heavy use, but the threshold for a "high" load average varies depending on the specific system and its configuration.

The following table presents the information that gives detail about the output of the top command:

Parameter Description
PID The process ID of the process
User The user name of the owner of the process
PR The priority of the process
NI The nice value of the process
VIRT The total amount of virtual memory used by the process
RES The amount of physical memory the process is using
SHR The amount of memory the process is sharing with other processes.
S The process status (D = interruptible sleep, R = running, S = sleeping, T = traced or stopped, or Z = zombies)
%CPU The share of CPU time that the process is using
%MEM The share of available physical memory the process is using
TIME+ The total CPU time the process has used since starting
COMMAND The command line name of the process (program started)

Signals

In Linux, one way to control processes is through the use of signals. Signals are predefined messages that processes recognize and can choose to ignore or act on. Many well-written applications are able to receive and respond to standard Unix process signals. System administrators use signals to stop or interrupt a process that may be hung up or taking too much CPU.

The following table describes each signal.

Signal Name Description
1 HUP Hangs up
2 INT Interrupts
3 QUIT Stops running
9 KILL Unconditionally terminates
11 SEGV Produces segment violation
15 TERM Terminates if possible
17 STOP Stops unconditionally, but doesn't terminate
18 TSTP Stops or pauses, but continues to run in background
19 CONT Resumes execution after STOP or TSTP

Killing processes

The kill and killall command in Linux allows you to send signals to processes based on their process ID(PID) or name respectively. The default signal sent is TERM, which requests the process to stop running. The kill command can also use the -s parameter to specify other signals such as HUP. The killall command is useful when you want to stop multiple processes by using their names with wildcard characters. To check if the command was effective, you need to run another ps or top command to check if the offending process stopped.

By understanding the commands and concepts presented in this article, you will be well equipped to manage and control processes in Linux.

Resources

Nemeth, E., Snyder, G., Hein, T., Whaley, B., & Mackin, D. (2018). Unix and Linux system administration handbook (5th edition).

Related Content
Author
I’m a passionate full-stack software engineer, architect and Debian lover.