Process management is a fundamental aspect of Linux system administration, allowing you to monitor, control, and optimize the running processes on your system. This page covers the key commands and concepts related to managing processes in Linux.
A process is an instance of a program running on your system. Each process is assigned a unique Process ID (PID) and operates independently, though processes can also create child processes. Processes can be in various states, such as running, sleeping, or stopped.
-
Foreground Processes: These processes run directly under user control, often launched from a terminal, and block the terminal until they complete.
-
Background Processes: These processes run independently of the terminal, allowing the terminal to be used for other tasks while the process continues running.
-
Daemon Processes: These are background processes that typically start at boot and run continuously, providing services like web serving or database management.
The ps command provides a snapshot of currently running processes. It is highly versatile and can be used to display detailed process information.
-
Basic Process List:
ps
This shows processes running in the current terminal.
-
Detailed Process List:
ps aux
This command displays a detailed list of all running processes, including those not associated with the current terminal.
-
Filter by Process Name:
ps aux | grep process_nameThis command filters the process list to show only processes matching
process_name.
The top command provides a dynamic, real-time view of system processes. It displays resource usage and allows for process management.
-
Start
top:top
This launches the
topinterface, showing processes, CPU usage, memory usage, and more. -
Interactive Commands:
k: Kill a process by entering its PID.r: Renice (change the priority of) a process.q: Quit thetopinterface.
htop is a more user-friendly version of top, offering a color-coded, interactive interface for monitoring processes.
-
Install
htop(if not already installed):sudo apt install htop
-
Start
htop:htop
This opens the
htopinterface, where you can scroll, sort, and manage processes interactively.
The kill command sends signals to processes, typically to terminate them.
-
Kill a Process by PID:
kill PIDThis sends the default
SIGTERMsignal to gracefully terminate the process with the specified PID. -
Force Kill a Process:
kill -9 PIDThis sends the
SIGKILLsignal to forcibly terminate the process.
pkill allows you to send signals to processes based on their name, rather than their PID.
-
Kill Processes by Name:
pkill process_name
This sends the
SIGTERMsignal to all processes with the specified name.
killall kills all instances of a process by name.
-
Kill All Instances of a Process:
sudo killall process_name
This command kills all processes with the specified name.
The nice command is used to start a process with a modified scheduling priority, while renice changes the priority of an existing process.
-
Start a Process with a Lower Priority:
nice -n 10 commandThis starts the command with a nice value of 10, making it less CPU-intensive.
-
Change Priority of a Running Process:
sudo renice 10 -p PID
This changes the priority of the process with the specified PID.
vmstat reports information about processes, memory, paging, block IO, traps, and CPU activity.
-
Basic Usage:
vmstat
This displays a summary of the current system performance.
-
Continuous Monitoring:
vmstat 5
This updates the performance summary every 5 seconds.
iostat reports CPU and I/O statistics, useful for identifying bottlenecks in disk performance.
-
Basic Usage:
iostat
This displays the average CPU and I/O statistics since the last reboot.
-
Real-Time Monitoring:
iostat 5
This updates the statistics every 5 seconds.
You can run a command in the background by appending & at the end.
-
Start a Process in the Background:
command &
This starts the command in the background, allowing you to continue using the terminal.
-
List Background Jobs:
jobsThis lists all jobs running in the background.
-
Bring a Job to the Foreground:
fg %job_numberThis brings the specified background job to the foreground.
-
Send a Job to the Background:
bg %job_numberThis sends a suspended job to the background.
Process management is an essential skill for any Linux user or administrator. By understanding how to view, manage, and prioritize processes, you can ensure that your system runs smoothly and efficiently. Mastering these commands will allow you to take control of the processes on your system, whether you're optimizing performance or troubleshooting issues.
Next: Scheduling Tasks (Cron)
Previous: Managing Services