Advanced Process Management Techniques
Selective termination requires sophisticated tools that provide granular control over process management in Linux systems.
graph TD
A[Selective Termination Tools] --> B[Command-Line Tools]
A --> C[System Utilities]
A --> D[Programming Interfaces]
pkill and pgrep
## Terminate processes by name
pkill firefox
## Find processes matching specific criteria
pgrep -u username chrome
## Interactive process management
htop
## Killing processes directly from interface
## Press 'k' and enter PID
Sophisticated Termination Strategies
Filtering Processes
Criteria |
Command |
Example |
By User |
pkill -u username |
pkill -u john |
By CPU Usage |
pkill -f '%cpu>80' |
Terminate high-load processes |
By Memory |
pgrep -f 'rss>1000' |
Find memory-intensive processes |
Advanced Scripting Techniques
Selective Termination Script
#!/bin/bash
## Function for intelligent process termination
selective_terminate() {
local process_name=$1
local max_memory=${2:-500} ## Default 500MB
## Find processes exceeding memory threshold
pids=$(ps aux | grep $process_name \
| awk -v max=$max_memory '$6 > max {print $2}')
for pid in $pids; do
echo "Terminating $process_name with PID $pid"
kill -15 $pid
sleep 2
## Forceful kill if not responding
if ps -p $pid > /dev/null; then
kill -9 $pid
fi
done
}
## Example usage
selective_terminate chrome 800
systemctl and systemd
## List running services
systemctl list-units
## Selectively stop services
systemctl stop specific_service
Programmatic Process Control
Python Process Management
import psutil
def terminate_by_criteria(criteria_func):
for proc in psutil.process_iter(['pid', 'name']):
if criteria_func(proc):
proc.terminate()
## Example: Terminate all Python processes
terminate_by_criteria(lambda p: p.info['name'] == 'python3')
Best Practices
- Always verify process details before termination
- Use least invasive termination signals
- Implement logging for tracking
- Consider process dependencies
LabEx Learning Approach
At LabEx, we recommend hands-on practice with process management tools in controlled Linux environments to build practical skills.
Error Handling and Safety
- Implement proper error checking
- Use sudo/root permissions carefully
- Provide user confirmations for critical terminations