In this guide, you will learn how Linux handles processes and discover various techniques to diagnose and manage them effectively. Every command you execute creates a process that runs until it completes its task or is manually terminated. For instance, running a command like “ls” creates a short-lived process that displays the directory contents and then exits, while services like the SSH daemon run continuously in the background. Below is an example that lists all files—including hidden ones—using the ls command:Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Using the ps Command
Theps command is a fundamental utility for inspecting active processes. It has several option syntaxes, reflecting both Unix and BSD styles. For example, ps -a uses Unix-style options, while ps -A (uppercase A) follows BSD syntax, leading to different outputs.
Running ps without any options only displays processes associated with the current terminal session. To see all processes running on the system, combine the options a, x, and u:
- The
axoptions ensure that processes from all controlling terminals are listed. - The
uoption presents a user-oriented format, adding columns for memory and CPU usage as well as the process owner. This is whyps auxis widely used for obtaining a complete snapshot of system processes.
Analyzing the ps aux Output
Review the typical columns shown in theps aux output:
| Column | Description |
|---|---|
| %CPU | Percentage of one CPU core’s capacity used by the process. A value of 150 may indicate one full core plus 50% of a second core. |
| %MEM | Percentage of the system’s total memory used by the process. |
| START | The time or date when the process was initiated. |
| TIME | Total CPU time consumed by the process. Processes with long lifespans often spend the majority of their time sleeping. |
| COMMAND | The command that initiated the process, along with its parameters. |
[kthreadd]) are kernel processes operating in privileged areas and usually do not require user interaction. Only processes outside these brackets represent user-space applications.
Monitoring Processes in Real Time with top
Whileps gives you a static snapshot, the top command is ideal for real-time process monitoring. It continuously updates its display and reorders the processes by CPU usage, making it easier to identify resource-intensive processes.
Use the arrow keys to navigate the list, and press “Q” to exit. Below is an example output of the top command:
-U option:
pgrep command with the -a option to display the complete command line:
Adjusting Process Priority Using Niceness and renice
Linux processes have a “niceness” value that determines their scheduling priority. This value ranges from -20 (highest priority) to 19 (lowest priority). When launching a process, you can assign a niceness value. For example, launching a bash shell with a niceness value of 11:ps aux output does not include niceness values. To view these, use the BSD long format with ps l, which displays the NI column:
renice command. For example, to change the nice value of a process with PID 12238 from 0 to 7:
Only superusers can lower the nice value (i.e., increase priority) of a process. Regular users can only increase the niceness value.
Sending Signals to Processes
Linux signals prompt processes to perform actions such as stopping, pausing, or terminating. Although some signals can be handled gracefully by applications, signals like SIGSTOP and SIGKILL are non-catchable. SIGSTOP pauses a process until it receives a SIGCONT, while SIGKILL immediately terminates it. List all available signals using:sudo:
kill command sends SIGTERM by default for a graceful shutdown. To forcefully terminate a process, you can use SIGKILL in any of these ways:
pkill command lets you send signals based on process names. For example, to send SIGKILL to all processes with “bash” in their name, first verify the target processes:
Be very careful when terminating processes with signals such as SIGKILL. Stopping critical processes may lead to system instability or disconnect you from your session.
Managing Background and Foreground Processes
Long-running tasks can be executed in the background, allowing you to use your terminal for other commands. For example, to run a sleep command for 180 seconds:fg or continue running it in the background using bg.
Checking Open Files with lsof
Thelsof command lets you inspect which files or directories are in use by a process. First, obtain the PID of your bash shell:
Conclusion
This guide has demonstrated multiple techniques for diagnosing and managing processes in Linux. You learned to inspect processes withps and top, adjust process priorities using nice and renice, send signals with kill and pkill, manage background and foreground tasks, and inspect open files with lsof. Mastering these tools is essential for effective system administration and troubleshooting on Linux.
For more in-depth knowledge, explore additional resources such as: