Linux: Manage System Processes
This tutorial dives into the practical aspects of managing processes in Linux, a handy skill for troubleshooting and resource management.
Jul 21st, 2024 6:00am by
Feature image by Dariusz Robert Drewnicki from Pixabay.
Understand Processes
Processes are programs in execution on the system. They consist of several components, including:- Program instructions: Machine code instructions processed by the CPU.
- Data: Information manipulated by the process.
- Resources: Processor time, memory space, storage space and network connectivity consumed by the process.
- Process ID (PID): A unique identity for the process. The PID allows administrators to reference the process to manage it.
- Parent Process ID (PPID): A process that spawned the process you’re working with.
- Hierarchy of parent and child processes.
- Scheduling for access to system resources (mainly the CPU).
- Isolation of process address spaces for stability and security.
What Administration Do Processes Need?
Most of the time, Linux users and administrators manage applications by starting and stopping them with commands or graphical icons. Launching these applications initiates one or more processes. Exiting the program ends those processes. Here are the typical ways of starting and stopping the man page application to show the help file for the ls command.
$ man ls
What Are Process IDs?
Process IDs (PIDs) are unique numbers assigned to processes by the Linux kernel when the process spawns (starts). PIDs are important because they are a way to reference the process for attention by the administrator. The administrator may need to know how much memory or processor time the process consumes. The administrator may also need to end the process manually if it does not quit correctly.
Figure 1: Partial output from the ps command showing a man page process. The process ID is in the second column (340543).
Display and Manage Processes
The primary process management command is ps . This flexible and powerful command displays processes and offers many options. List all processes along with supplementary information:
$ ps -ef
Figure 2: Partial output from the ps -ef command.
$ ps -u username
Managing Jobs
Programs typically run in the foreground, meaning they consume the interface and both you and the system focus on them. However, it is possible to execute processes in the background. This causes them to run but permits you to continue using the command-line interface (CLI) to accomplish other tasks. To foreground and background processes, you must work with a different set of identifiers. Specifically, these are job IDs. Job IDs are slightly different from process IDs. Process IDs are labeled systemwide, while job IDs are identified on a per-user basis. All jobs are processes, but not all processes are jobs (since some processes are not assigned to specific users). When you first run a job, it executes in the foreground, consuming the shell and preventing you from running other commands. This could be an issue if you’re launching a long backup job or another task that takes a while to run. You can interrupt a running process using Ctrl-Z and then type bg to background it. You can launch a program directly into the background by typing the program name and adding the & character.
$ man ls &
Figure 3: The jobs command displays the current man page job.
$ fg %1
Figure 4: Identifying the job number (1) for the man page job, then moving it to the foreground.
Use grep to Filter ps Results
Production Linux systems may have thousands of processes running simultaneously, making browsing or searching the ps command output difficult. You can combine ps with a filtering or pattern-matching utility named grep to find exactly what you’re looking for. The syntax for using ps and grep together looks like this:
$ ps -ef | grep process_name
- ps -ef : The ps command with the options you want.
- | : The “pipe” character takes the output of the first command and uses it as the input of the second command. In this case, it takes the results of the ps command (a list of all processes) and makes it the input of the grep command (a search tool).
- grep process_name : The grep command searches the ps results for the process_name you specified, filtering or narrowing the output to something more manageable.
Figure 5: Adding the grep pattern matcher to ps to filter results.
$ ls /etc | grep -i net
Figure 6: An example of using grep with other commands, such as ls.
Kill Processes
Most applications will exit gracefully, meaning they return their CPU time and memory address to the operating system and end their own process. Sometimes, an application does not exit gracefully, and the administrator must end the process using the kill command. The kill command can send various signals to the application, but the most important is the -9 or SIGKILL . This signal unequivocally ends the process, forcing it to close and return its resources to the system. Be aware that you will lose any unsaved data if you end a program this way. Killing a process is usually a last resort. The command looks like this (assuming PID 9876):
$ kill -9 9876
- SIGINT (2 ): Interrupts or ends the process (Ctrl+C).
- SIGTERM (15 ): Ends the process gracefully.
Understand Zombie Processes
One administrative task you may encounter is eliminating zombie processes. Zombie processes are leftover remaining components from programs that are no longer running but did not properly end all processes. These zombie processes continue to consume process IDs when they no longer support an application. This isn’t normally a problem, but you should be aware of them. You’ll need to clean up zombie processes by ending the parent processes. Again, zombie processes don’t consume many CPU or memory resources, so don’t be too concerned.Other Commands
The main process management tool is ps , but several other useful tools exist for manipulating processes. These tools include pgrep , pidof and pstree .The pgrep Command
The pgrep command combines the ps and grep utilities to simplify searching for specific processes. Add the -l option to display the actual process name with the PID. For example, to search for processes related to the SSH utility, type:
$ pgrep -l ssh
Figure 7: The pgrep command displays process information for SSH.
The pidof Command
The pidof command shows PID information for running applications. The syntax is the pidof command plus the program name.
$ pidof program_name
Figure 8: The pidof command displays process information for sshd.
The pstree Command
Some applications are complex enough to require more than one process. Others dedicate new processes to each new network connection or service request. These processes are called parent processes, and the subprocesses they start are called child processes. When examining system performance, displaying a parent process and its related child processes in a visual format may be helpful. The pstree command provides that capability. With no argument or options specified, pstree displays all processes on the system.
$ pstree
Figure 9: Partial output from the pstree command.
$ pstree 9876
$ pstree username
$ pstree -p username
Figure 10: Partial output from the pstree command showing processes used by the damon account.
Other Process Commands
The top utility also displays process information. While top is usually considered a performance monitoring utility, processes consume resources, impacting the system’s performance. You might use top with the ps command to determine which processes are causing issues.
Figure 11: The upper portion of the top command displays process information, while the lower portion displays actual processes.
Wrap Up
Linux users and administrators will periodically need to manage the code running on their systems. Code being executed by the CPU is called a process, and you can display processes and related information using the ps command. One critical component of process management is the process ID — the label by which the system identifies the process. You’ll use this PID to investigate or kill the process if necessary. Other Linux tools also work with processes, including pgrep , pidof , pstree and top . Use these tools to learn more about what’s running on your system and troubleshoot issues.
YOUTUBE.COM/THENEWSTACK
Tech moves fast, don't miss an episode. Subscribe to our YouTube
channel to stream all our podcasts, interviews, demos, and more.