1. Process Overview
A process is an execution instance of a program and the basic unit for operating system resource allocation and scheduling. In Linux systems, each run program creates one or more processes, which occupy system resources (such as CPU, memory, disk I/O, etc.). Understanding process concepts and management methods is crucial for Linux system administration and performance optimization.
1.1 Process Lifecycle
- Creation: New processes are created through the fork() system call.
- Ready: The process is ready to run, waiting for CPU allocation.
- Running: The process is executing on the CPU.
- Blocked: The process is waiting for an event to complete (such as an I/O operation).
- Termination: The process has completed execution or been terminated.
1.2 Process States
- R (Running): The process is run or waiting in the ready queue.
- S (Sleeping): The process is in an interruptible sleep state, waiting for an event to complete.
- D (Disk Sleep): The process is in an uninterruptible sleep state, usually waiting for an I/O operation to complete.
- Z (Zombie): The process has terminated, but the parent process has not yet reclaimed its resources.
- T (Stopped): The process has been paused.
- X (Dead): The process is dead, which is a transient state and usually not visible in process lists.
2. Process Viewing Commands
Linux provides multiple commands for viewing process information in the system, which can help us understand process status, resource using, and more.
2.1 ps Command
ps (Process Status) command is used to display process information in the current system.
# View processes in current terminal ps # View all processes ps aux # View all processes in tree structure ps -ef --forest # View processes of specific user ps -u username # View specific process ps -p PID # Sort by CPU using ps aux --sort=-%cpu # Sort by memory using ps aux --sort=-%mem # View thread information of process ps -L PID # Display detailed process information ps -ef | grep process_name
2.2 top Command
top (Table of Processes) command is used for real-time monitoring of process status and resource using in the system.
# Start top command top # Run top in batch mode top -b # Set top refresh interval (seconds) top -d 1 # Show only processes of specific user top -u username # Show only processes with specific PIDs top -p PID1,PID2 # Sort by CPU using top -o %CPU # Sort by memory using top -o %MEM # Show thread information top -H # Interactive keys in top # h: Show help information # k: Terminate specified process # r: Modify process priority # q: Exit top # 1: Show using of all CPU cores # M: Sort by memory using # P: Sort by CPU using # T: Sort by cumulative run time # c: Show complete command line
2.3 htop Command
htop is an enhanced version of top, providing a more user-friendly interface and more features.
# Install htop sudo apt install htop # Debian/Ubuntu sudo yum install htop # CentOS/RHEL # Start htop top # Interactive keys in htop # F1: Show help information # F2: Set up htop # F3: Search processes # F4: Filter processes # F5: Display in tree structure # F6: Select sort method # F7: Decrease process priority # F8: Increase process priority # F9: Terminate process # F10: Exit htop # Spacebar: Mark process # u: Show processes of specific user # l: Show files opened by process # s: Show system calls of process
2.4 pgrep and pkill Commands
pgrep command is used to find processes by process name or other attributes, and pkill command is used to terminate processes by process name or other attributes.
# Find processes with specific name pgrep process_name # Find processes of specific user pgrep -u username # Find processes and show complete command line pgrep -l process_name # Find processes and show process ID and complete command line pgrep -a process_name # Terminate processes with specific name pkill process_name # Terminate processes of specific user pkill -u username # Terminate processes of specific terminal pkill -t terminal
2.5 pidof Command
pidof command is used to find the process ID of a specified program.
# Find process ID of specified program pidof program_name # Find process ID of specified program, separated by commas pidof -c program_name # Find process ID of specified program, separated by spaces pidof -s program_name
3. Process Control Commands
Linux provides multiple commands for controlling process execution, including starting, terminating, pausing, and resuming processes.
3.1 kill Command
kill command is used to send signals to processes, most commonly to terminate processes.
# Terminate process with specified PID kill PID # Force terminate process kill -9 PID # Send SIGTERM signal to process (default) kill -15 PID # Send SIGHUP signal to process kill -1 PID # Send SIGINT signal to process kill -2 PID # Send SIGSTOP signal to process kill -19 PID # Send SIGCONT signal to process kill -18 PID # Terminate all processes with specified name kill $(pgrep process_name) # View available signals trap -l kill -l
3.2 killall Command
killall command is used to terminate processes by process name.
# Terminate all processes with specified name killall process_name # Force terminate all processes with specified name killall -9 process_name # Terminate all processes matching specified name (case insensitive) killall -I process_name # Terminate all processes of specified user killall -u username # Only terminate processes run longer than specified time killall -o 10m process_name # Only terminate processes run less than specified time killall -y 10m process_name
3.3 pkill Command
pkill command is used to terminate processes by process name or other attributes.
# Terminate all processes with specified name pkill process_name # Force terminate all processes with specified name pkill -9 process_name # Terminate all processes matching specified pattern pkill -f pattern # Terminate all processes of specified user pkill -u username # Terminate all processes of specified terminal pkill -t terminal
3.4 nice and renice Commands
nice command is used to set process priority, and renice command is used to modify the priority of run processes.
# Start process with specified priority nice -n 10 command # Start process with highest priority nice --19 command # Modify priority of run process renice 10 -p PID # Modify priority of all processes of specific user renice 5 -u username # Modify priority of all processes of specific group renice 0 -g groupname
4. Job Control
Job control refers to managing the execution of multiple processes in a single terminal session, including placing processes in foreground or background execution, pausing and resuming processes, etc.
4.1 Basic Job Control Commands
# Start process in background command & # Pause current process (Ctrl+Z) # Then can use fg or bg commands to continue execution # View all jobs jobs # Move background job to foreground fg %job_id # Continue paused job in background bg %job_id # Terminate background job kill %job_id # Run process in background and ignore SIGHUP signal nohup command & # Run process in background and redirect output to file command > output.log 2>&1 & # Run process in background and detach from terminal setsid command # Create session using screen or tmux screen # or tmux
4.2 screen Command
screen command is used to create multiple sessions, each of which can run different commands and can be switched between different terminals.
# Install screen sudo apt install screen # Debian/Ubuntu sudo yum install screen # CentOS/RHEL # Create new session screen # Create named session screen -S session_name # List all sessions screen -ls # Attach to session screen -r session_name # Detach session (Ctrl+A, d) # Terminate session exit # Force terminate session screen -S session_name -X quit
4.3 tmux Command
tmux is a terminal multiplexer, similar to screen but providing more features.
# Install tmux sudo apt install tmux # Debian/Ubuntu sudo yum install tmux # CentOS/RHEL # Create new session tmux # Create named session tmux new -s session_name # List all sessions tmux ls # Attach to session tmux attach -t session_name # Detach session (Ctrl+B, d) # Terminate session exit # Force terminate session tmux kill-session -t session_name # Create new window in session (Ctrl+B, c) # Switch between windows (Ctrl+B, 0-9) # Create new pane in session (Ctrl+B, % or Ctrl+B, ") # Switch between panes (Ctrl+B, arrow keys)
5. System Resource Monitoring
System resource monitoring is an important part of Linux system administration, which can help us understand the system's run status and timely discover and solve problems.
5.1 System Load Monitoring
# View system load uptime # View system load and process status top # View historical system load records mpstat # View load of each CPU core mpstat -P ALL
5.2 Memory Usage Monitoring
# View memory using free -h # View detailed memory using information cat /proc/meminfo # View process memory using top -o %MEM # View memory using of specific process pmap PID # View process memory map cat /proc/PID/maps
5.3 Disk I/O Monitoring
# View disk I/O status iostat # View detailed disk I/O information iostat -x # View I/O status of specific disk iostat -x /dev/sda # Real-time monitor disk I/O iotop # View I/O status of processes iotop -p PID # View disk using df -h # View directory size du -h directory
5.4 Network I/O Monitoring
# View network interface statistics netstat -i # View network connection status netstat -tuln # View network interface traffic ifconfig # Real-time monitor network traffic tcpdump # Real-time monitor network connections and traffic netstat -ant # View process network connections lsof -i # View network connections of specific process lsof -i -p PID
5.5 Comprehensive Monitoring Tools
# Install sar sudo apt install sysstat # Debian/Ubuntu sudo yum install sysstat # CentOS/RHEL # View CPU using sar -u # View memory using sar -r # View disk I/O status sar -d # View network I/O status sar -n DEV # View system comprehensive performance tuned-adm list # Install and use atop sudo apt install atop # Debian/Ubuntu sudo yum install atop # CentOS/RHEL # Start atop atop
6. Process managementment Practice
6.1 Case Objective
Monitor process status in the system, identify and handle processes that occupy excessive resources, and ensure stable system operation.
6.2 Implementation Steps
6.2.1 Monitor System Load
# View system load uptime # Real-time monitor system status top
6.2.2 Identify Processes Occupying Excessive Resources
# View processes with highest CPU using ps aux --sort=-%cpu | head -10 # View processes with highest memory using ps aux --sort=-%mem | head -10 # View processes with highest disk I/O using iotop -o -b | head -10
6.2.3 processing Processes Occupying Excessive Resources
# Terminate process occupying excessive resources kill PID # Force terminate process kill -9 PID # Modify process priority renice 10 -p PID # Run process in background command > output.log 2>&1 & # Run long-term task using screen or tmux screen -S task_name # Then run command in screen session # Press Ctrl+A, d to detach session
6.2.4 Monitor Process Status
# View process status ps -p PID -o pid,ppid,state,command # View detailed process information cat /proc/PID/status # View files opened by process lsof -p PID # View network connections of process netstat -antp | grep PID
7. Interactive Exercises
Exercise 1: Process Viewing
Perform the following operations:
- 1. Use ps command to view all processes in the system.
- 2. Use ps command to view processes of specific user.
- 3. Use top command to real-time monitor process status in the system.
- 4. Use pgrep command to find processes with specific name.
- 5. Use pidof command to find process ID of specific program.
Exercise 2: Process Control
Perform the following operations:
- 1. Start a long-run process (such as sleep 3600).
- 2. Use kill command to terminate the process.
- 3. Start multiple processes with the same name.
- 4. Use killall command to terminate all these processes.
- 5. Start processes with different priorities and observe their execution.
Exercise 3: Job Control
Perform the following operations:
- 1. Start a process in the background.
- 2. Use jobs command to view job list.
- 3. Move background job to foreground.
- 4. Pause foreground process, then move it to background to continue execution.
- 5. Use nohup command to start a process so it continues run after terminal closure.
Exercise 4: System Resource Monitoring
Perform the following operations:
- 1. Use iostat command to view disk I/O status.
- 2. Use free command to view memory using.
- 3. Use netstat command to view network connection status.
- 4. Use sar command to view system comprehensive performance.
- 5. Identify the process that occupies the most resources in the system and analyze the reason.