Process management is the process of managing and controlling the execution of processes in a computer system. A process is a program in execution, which consists of a series of instructions and data that are executed by the CPU. In a multi-tasking operating system, multiple processes can be executed simultaneously, and process management is responsible for allocating system resources such as CPU time, memory, and I/O devices to these processes.
The main goals of process management are to ensure that processes are executed efficiently, that they do not interfere with each other, and that they are terminated gracefully when they are no longer needed. To achieve these goals, process management provides various services such as process creation, process scheduling, process synchronization, and process termination.
The following commands allow you to view information about running processes, change their priority, and terminate them if necessary.
ps
The 'ps' command is used to display information about the currently running processes on a Linux system.
The basic syntax for using 'ps' is:
Some common options for the 'ps' command include:
- '-e' or '--every': display information about all processes, not just those started by the current user.
- '-f' or '--forest': display the process hierarchy as a tree.
- '-u' or '--user': display information about processes for a specific user.
- '-A' or '--all': display information about all processes, not just those that are running.
By default, 'ps' displays information about the process ID (PID), the terminal associated with the process, the process owner, the process status, and the command used to start the process.
The 'ps' command is a basic and powerful tool for investigating and managing processes on a Linux system and is commonly used in combination with other commands like 'kill' or 'top' to control the behavior of processes and the allocation of system resources.
top
The 'top' command is a real-time system monitoring utility in Linux that displays information about the system's running processes.
The basic syntax for using 'top' is:
When run, 'top' displays a continuously updated list of processes, showing their current status, the amount of CPU and memory resources they are using, and other information such as the user running the process and the process ID (PID).
Some common options for the 'top' command include:
- '-d' or '--delay': specify the delay between updates in seconds.
- '-p' or '--pid': display information about a specific process ID.
- '-u' or '--user': display information about processes for a specific user.
By default, 'top' sorts the process list by the number of CPU resources being used, but you can sort by other criteria, such as memory usage or process ID, by pressing certain keys while 'top' is running.
Its usage is simple, you just type top, and the terminal will be fully immersed in this new view:
The process is long-running. To quit, you can type the q letter or Ctrl + C.
There's a lot of information being given to us: the number of processes, how many are running or sleeping, the system load, the CPU usage, and a lot more.
Below, the list of processes taking the most memory and CPU is constantly updated. By default, as you can see from the %CPU column highlighted, they are sorted by the CPU used.
The 'top' command is a powerful tool for understanding how system resources are being used and for identifying processes that may be causing performance issues. It is often used by system administrators and developers to diagnose and resolve performance problems on Linux systems.
htop
'htop' is an alternative to the 'top' command in Linux. It is a real-time system monitoring utility that provides a more user-friendly interface and additional features compared to 'top'.
The basic syntax for using 'htop' is:
When run, 'htop' displays a continuously updated list of processes, showing their current status, the amount of CPU and memory resources they are using, and other information such as the process ID (PID), the user running the process, and the command used to start the process.
Some of the key features of 'htop' include:
- A color-coded interface that makes it easier to quickly identify processes and system resources.
- The ability to sort the process list by multiple criteria, such as CPU usage, memory usage, process ID, and more.
- A detailed process view that shows information about a specific process, including its full command line, environment variables, and more.
- A system status bar that provides information about system load, memory usage, and other key metrics.
- Support for mouse-based navigation and process management, making it easier to interact with the utility.
'htop' is a useful tool for system administrators and developers who need to monitor the performance of Linux systems and manage processes in real time. It provides a more user-friendly interface and additional features compared to 'top', making it a popular alternative for system monitoring on Linux.
kill
The 'kill' command is a Linux utility that is used to send a signal to a process. It can be used to terminate a process, stop a process, or resume a stopped process. The syntax for using the 'kill' command is as follows:
Some common signals that can be sent using the 'kill' command include:
- SIGTERM (terminate): This is the default signal sent by 'kill' and is used to request that a process terminate gracefully.
- SIGKILL (kill): This signal cannot be ignored or caught by the process, and is used to force a process to terminate immediately.
- SIGSTOP (stop): This signal stops a process, and it cannot be resumed until a SIGCONT signal is sent.
- SIGCONT (continue): This signal resumes a stopped process.
It's not just used to terminate a program like a name would suggest, but that's its main job. We use it in this way:
By default, this sends the TERM signal to the process id specified. We can use flags to send other signals, including:
- HUP means to hang up. It's sent automatically when a terminal window that started a process is closed before terminating the process.
- INT means to interrupt, and it sends the same signal used when we press ctrl-C in the terminal, which usually terminates the process.
- KILL is not sent to the process, but to the operating system kernel, which immediately stops and
terminates the process. - TERM means terminate. The process will receive it and terminate itself. It's the default signal sent by kill .
- CONT means continue. It can be used to resume a stopped process.
- STOP is not sent to the process, but to the operating system kernel, which immediately stops (but does not terminate) the process.
You might see numbers used instead, like kill -1 <PID>.
In this case,
- 1 corresponds to HUP
- 2 corresponds to INT
- 9 corresponds to KILL
- 15 corresponds to TERM
- 18 corresponds to CONT
- 15 corresponds to STOP
It's important to note that the 'kill' command should be used with caution, as it can cause data loss or corruption if used improperly. Before using the 'kill' command to terminate a process, it's a good idea to try and determine what the process is doing, and if possible, to try and terminate it gracefully using SIGTERM first.
pkill
The 'pkill' command is a Linux utility that is used to send a signal to a process based on its name, rather than its process ID. It is similar to the 'killall' command, but with some key differences.
The syntax for using the 'pkill' command is as follows:
Where "pattern" is a string that matches the name of the process you want to signal, and options are optional arguments that specify the type of signal to be sent and other behavior of the 'pkill' command.
Some common options that can be used with 'pkill' include:
- -signal: Specifies the type of signal to be sent. If no signal is specified, the default signal SIGTERM (terminate) is sent.
- -f: Matches the process name against the full command line, rather than just the process name.
- -u: username: Limits the search for processes to those owned by the specified user.
- -n: Matches the most recently started process.
killall
The 'killall' command is a Linux utility that is used to send a signal to multiple processes based on their names. It is similar to the 'pkill' command, but with some key differences. The syntax for using the 'killall' command is as follows:
Where name is the name of the process you want to signal, and options are optional arguments that specify the type of signal to be sent and other behavior of the 'killall' command.
Some common options that can be used with 'killall' include:- -signal: Specifies the type of signal to be sent. If no signal is specified, the default signal SIGTERM (terminate) is sent.
- -e: Matches the exact process name, rather than a partial match.
- -I: Matches the process name exactly, ignoring the case.
- -u username: Limits the search for processes to those owned by the specified user.
nice
The 'nice' command is a Linux utility used to set the priority of a process. By default, processes run with a priority of 0, but the 'nice' command allows you to specify a different priority level, which can range from -20 (highest priority) to 19 (lowest priority). The higher the priority of a process, the more CPU time it will receive.
The syntax for using the 'nice' command is as follows:
Where options are optional arguments that specify the priority level and other behavior of the 'nice' command, and command [arguments] is the command you want to run with the specified priority.
If no priority level is specified, the default priority level of 10 is used. To specify a different priority level, use the -n option followed by the priority level:
For example, to run the 'ls' command with a priority level of 5, you would use the following command:
Note that the 'nice' command only affects the priority of a process, not the amount of memory or other resources it can access. Additionally, only processes run by the superuser can increase their priority, while all users can decrease the priority of their own processes.
renice
The 'renice' command is a Linux utility used to change the priority of an already running process. It allows you to adjust the priority of a process, which determines how much CPU time the process will receive. By default, processes run with a priority of 0, but the 'renice' command allows you to specify a different priority level, which can range from -20 (highest priority) to 19 (lowest priority).
The syntax for using the 'renice' command is as follows:
Where priority is the new priority level you want to set for the process, processID is the ID of the process you want to change the priority of, and -p is an optional argument that specifies that you want to change the priority of a process by its process ID, rather than by its name.
For example, to change the priority of the process with ID 12345 to a priority level of 5, you would use the following command:
Note that the 'renice' command only affects the priority of a process, not the amount of memory or other resources it can access. Additionally, only processes run by the superuser can increase the priority of other processes, while all users can decrease the priority of their own processes.
These are some of the basic process management commands in Linux. These commands provide a way to control and monitor the processes running on a Linux system and are an important tool for system administrators and power users.