Managing Linux processes: A step-by-step guide
Managing processes is a fundamental task for any Linux system administrator. Here are the basics of managing and monitoring them.
Sep 23, 2024 • 6 Minute Read
Whether you are using CentOS, Ubuntu or another distribution, understanding how to monitor and control processes is essential for maintaining Linux system performance and stability. This blog will guide you through the basics of managing and monitoring Linux processes.
What are Linux Processes?
In Linux, a process is an instance of a running program. Everything that happens on Linux, a process is started. Each process has a unique Process ID (PID) and is associated with a user. Processes can be in various states: running, sleeping, stopped, or zombie. Properly managing these processes involves monitoring their status, prioritizing them, and terminating them when necessary.
There are three process types, which are:
- Daemons; which provide services. These are usually started when a computer is booted and nearly always runs with root privileges.
- Shell jobs, which are started from the command line.
- Kernel threads; these are part of the Linux Kernel. You cannot manage these, but you can monitor them.
Monitoring Processes
On a Linux server there can be more than 100 active processes running at any time. With so much running, the chances of something going wrong with a process or more exists. We must be able to monitor them.
Using ps
The ps (process status) command in Linux is used to display information about active processes. It provides a snapshot of the current processes, showing details such as process IDs (PIDs), terminal associations, CPU usage, memory usage, and more.
Using just ps without an argument displays information about processes that have been started by the user running the command.
One of the most common and used commands is ps aux, which is used to provide a comprehensive view of all running processes. Here's what each part stands for:
- a: Show processes for all users.
- u: Display the process's user-oriented format, which includes the user, CPU usage, memory usage, start time, and command.
- x: Show processes that are not attached to a terminal.
When you run ps aux, it combines these options to provide a detailed listing of all processes on the system, regardless of which terminal they are attached to.
Within this example we can see that pluralsight (shown as plural+) is the user running this process, the PID is 2384, CPU usage is 0.0, memory used is 0.0 and the start time of the process was 09:52.
Using top
The top command in Linux is a powerful tool for real-time system monitoring and process management. It provides a dynamic, real-time view of the system's running processes, displaying detailed information about CPU and memory usage, as well as other important system statistics. It also gives us an overview of the most active processes currently running.
Top is also an interactive utility, you can see live updates on processes whilst you are within the prompt. We can also manage processes from this utility, as you will see below within this blog.
Using pgrep
pgrep is a command-line utility in Linux used to search for processes currently running on the system based on various criteria. It is particularly useful for system administrators and users who need to manage or monitor processes without manually sifting through the output of commands like ps or top
As seen within the example, pgrep has been used to search for the PID for sshd, which has returned 3 PIDs. This makes it much easier and quicker to find a PID for a specific service.
Managing Processes
Killing a process with the kill command
The kill command in Linux is used to send signals to processes. While its name suggests it is used to terminate processes, it can actually send various types of signals to processes, not just termination signals. The most commonly used signal with kill is SIGTERM (signal 15), which requests a graceful termination. If a process does not respond to SIGTERM, SIGKILL (signal 9) can be used to forcefully terminate the process.
In this example, the PID 1054 was deemed problematic. Using the kill command with the PID we are able to terminate it so that it is no longer active, as shown by the rerun of the ps aux | grep 1054 command.
Killing a process with the pkill command
The pkill command in Linux is used to send signals to processes based on their name or other attributes. It provides a more convenient and flexible way to manage processes compared to the kill command, which requires you to know the exact Process ID (PID).
In the example, the process ibus-daemon was killed using the process name.
Killing a process with the killall command
The killall command in Linux is used to send signals to all instances of a process by its name. Unlike kill and pkill, which can target individual processes based on PID or other attributes, killall targets all processes that match the specified name. This can be particularly useful for terminating multiple instances of a process with a single command.
A simple exam of using the killall command would be killall -9 example_process, which would forcefully terminate the process by the name of example_process.
Manipulate process priority with nice and renice commands
In Linux, you can manage the priority of processes using the nice and renice commands. These commands allow you to adjust the scheduling priority of processes, affecting their CPU usage relative to other processes.
When Linux processes start, they are given a default priority of 20. To change a process's priority at startup, use the nice command. To adjust the priority of an already running process, use the renice command.
To run the process ibus-daemon with a priority of value you use the above command. To verify this is running correctly use the command top -p `pgrep "ibus-daemon"`.
To change the priority value of an existing process we need to use the PID ID and specify the new value. We will use the command renice -n 5 -p 3308 which will set the priority to 5, for the PID 3308, which is our ibus-daemon shown above.
And to verify, we will use the same command as before:
As seen, the new value is 5.
Managing processes with top
Previously you saw how you can use top to monitor processes with a real-time updating prompt. With top, you can use several interactive commands to manage processes, with the following basics ones being often used by Linux administrators:
- Quit top: Press q to exit top.
- Sort by CPU Usage: By default, top sorts processes by CPU usage. To ensure this, press P.
- Sort by Memory Usage: Press M to sort the processes by memory usage.
- Kill a Process: To kill a process, press k, then enter the PID of the process you want to terminate and press Enter. You will be prompted to enter the signal to send (default is 15 for SIGTERM, which is graceful termination).
- Renice a Process: To change the priority (niceness) of a process, press r, then enter the PID of the process, followed by the new nice value (between -20 and 19).
Conclusion
Managing processes is a fundamental task for any Linux system administrator. Whether you are using CentOS, Ubuntu, or another distribution, understanding how to monitor and control processes is essential for maintaining system performance and stability. This guide has covered the basics of managing and monitoring Linux processes.
By mastering these commands and techniques, you can effectively monitor and manage processes, ensuring optimal performance and stability for your Linux system.
I would encourage you to check out these courses on Pluralsight to learn more about managing processes on Linux, but also many other Linux topics that administrators should know: