Execution Management
Process Management Fundamentals
Process management is a critical aspect of Linux system administration and programming. Understanding how to control, monitor, and optimize process execution is essential for efficient system performance.
Process States
stateDiagram-v2
[*] --> Running
Running --> Waiting
Waiting --> Running
Running --> Stopped
Stopped --> Running
Running --> Zombie
Zombie --> [*]
Process State Definitions
State |
Description |
Running |
Active execution |
Waiting |
Waiting for resource/event |
Stopped |
Suspended execution |
Zombie |
Completed but not removed |
Process Identification
Key Process Commands
## List processes
ps aux
## Real-time process monitoring
top
## Process tree view
pstree
## Get current process ID
echo $$
Background and Foreground Execution
Managing Process Execution
## Run in background
command &
## Move background process to foreground
fg %1
## List background jobs
jobs
## Send process to background
Ctrl+Z
Process Signals
Signal Handling
## Send termination signal
kill -9 PID
## List available signals
kill -l
Common Signals
Signal |
Number |
Description |
SIGTERM |
15 |
Graceful termination |
SIGKILL |
9 |
Forced termination |
SIGSTOP |
19 |
Pause process |
SIGCONT |
18 |
Resume process |
Process Prioritization
## Change process priority
nice -n 10 command
renice 15 -p PID
Priority Levels
graph LR
A[Lowest Priority -20] --> B[Normal Priority 0]
B --> C[Highest Priority 19]
Advanced Execution Control
Subprocess Management
## Execute command and wait
wait $PID
## Parallel execution
command1 & command2 &
wait
Resource Monitoring
## CPU and memory usage
htop
## Disk I/O monitoring
iotop
## Network connections
netstat -tuln
Execution Logging
## Log command output
command > logfile.log 2>&1
## Append to log
command >> logfile.log
Best Practices
- Monitor system resources
- Use appropriate signals
- Manage process priorities
- Implement error handling
- Log critical operations
Complex Execution Script
#!/bin/bash
execute_with_timeout() {
timeout 10s command || {
echo "Command timed out"
exit 1
}
}
background_task() {
while true; do
## Long-running background process
sleep 60
done
}
main() {
background_task &
background_pid=$!
execute_with_timeout
kill $background_pid
}
main
By mastering execution management techniques with LabEx, you'll gain comprehensive control over Linux system processes and improve overall system performance and reliability.