In the Linux system, a process is an instance of a program running, like an executing “task”. Managing processes helps us check which programs are running, monitor resource usage, and terminate them when they behave abnormally. For beginners, mastering a few core process management commands can quickly solve daily problems.
1. Viewing Processes: ps Command¶
Purpose: The ps command is the most basic process viewing tool, which lists the status of currently running processes in the system.
Basic Usage: Simply enter ps or with parameters. A common combination is ps -aux or ps -ef (a shows all user processes, u shows detailed user information, x shows processes without a terminal; e shows all processes, f shows the full format).
Example:
Entering ps -aux will display output similar to the following (simplified):
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 19560 1164 ? Ss 10:00 0:00 /sbin/init
root 2 0.0 0.0 0 0 ? S 10:00 0:00 [kthreadd]
...
Key Columns Explained:
- PID: Process ID (unique identifier, like an “ID number” for subsequent operations);
- USER: User running the process;
- %CPU/%MEM: CPU and memory usage percentages;
- STAT: Process status (R=Running, S=Sleeping, Z=Zombie process, etc.);
- COMMAND: The command that started the process.
2. Real-Time Process Monitoring: top Command¶
Purpose: top is more powerful than ps, as it dynamically updates process information in real-time, making it suitable for monitoring system load and resource changes.
Basic Usage: Simply enter top, and the interface will automatically refresh (default refresh every 3 seconds).
Common Operations:
- Press P: Sort by CPU usage from high to low;
- Press M: Sort by memory usage from high to low;
- Press k: Terminate a process (enter the target PID and press Enter);
- Press q: Exit top.
Note: top runs continuously; press q to exit. Do not directly close the terminal window, as the process may continue to occupy resources otherwise.
3. Terminating Processes: kill and killall¶
Purpose: When a program freezes or behaves abnormally, we need to terminate the process. kill terminates by PID, while killall terminates by process name.
1. kill Command¶
- Usage:
kill [signal] PID(default signal is 15, a normal termination; 9 = forced termination). - Example:
# First, find the target process PID via ps, e.g., Firefox's PID is 1234
ps -aux | grep firefox
# Terminate the process (try normal termination first)
kill 1234
# If the process is unresponsive, force termination
kill -9 1234
- Note:
-9is a “hard kill” that may cause data loss (e.g., unsaved files). Use it only when the program is unresponsive.
2. killall Command¶
- Usage:
killall [signal] process_name(terminate by name directly, no need to find the PID). - Example:
# Terminate all processes named "firefox"
killall -9 firefox
- Advantage: Suitable for scenarios where you know the process name but not the PID; more efficient than killing one by one with
kill.
4. Process Tree View: pstree¶
Purpose: Displays the parent-child relationships of processes in a tree structure, intuitively showing the hierarchical structure of programs.
Basic Usage: Enter pstree directly, or with parameters p (show PID) and a (show full path).
Example:
pstree -ap
# Output similar to:
systemd(1)───sshd(1234)───bash(5678)───pstree(9012)
Advantage: Quickly understand the dependency relationships between programs (e.g., bash is the parent process of pstree).
5. Background Job Management: jobs, bg, and fg¶
Scenario: When running a program in the terminal, you don’t want to occupy the foreground (e.g., compiling code, downloading large files).
1. jobs Command¶
- Purpose: View background suspended jobs (tasks paused with
Ctrl+Z).
2. bg and fg¶
bg: Resume a suspended background job (bg %job_number; the job number is shown byjobs).fg: Bring a background job back to the foreground (fg %job_number).
Example:
# Start a long-running task (e.g., compilation) and run it in the background
make -j8 &
# If you want to pause it midway, press Ctrl+Z; the terminal will show: [1]+ Stopped make -j8
# View background jobs: jobs
# Resume background execution: bg 1
# Bring to foreground: fg 1
Beginner’s Notes¶
- Do not terminate critical system processes: Such as
init,systemd,sshd, etc. Terminating these may crash the system. - Confirm PID before operation: Use
psorpgrep(find PID by name) to confirm the process PID to avoid accidental termination. - Ordinary user permissions: Ordinary users can only terminate processes they started. To terminate processes of other users, use
sudoor root privileges.
Mastering the above commands allows you to handle 90% of process management scenarios in the Linux system. In daily work, ps for checking processes, top for resource monitoring, and kill for terminating abnormal processes are the most core combination. When encountering issues, use man [command] to view detailed manuals (e.g., man ps), and practice regularly to become proficient!