| name | process-management |
| description | Production-grade process management - jobs, signals, cron, systemd |
| sasmp_version | 1.3.0 |
| bonded_agent | 04-process-management |
| bond_type | PRIMARY_BOND |
| version | 2.0.0 |
| difficulty | intermediate |
| estimated_time | 5-7 hours |
Process Management Skill
Master process control, signals, scheduling, and monitoring
Learning Objectives
After completing this skill, you will be able to:
Prerequisites
- Bash basics
- Linux system fundamentals
- User permissions understanding
Core Concepts
1. Process Inspection
ps aux
ps -ef
ps --forest
pgrep -f "pattern"
pidof nginx
ps aux | grep '[n]ginx'
top
htop
2. Signal Handling
kill PID
kill -9 PID
kill -HUP PID
killall nginx
pkill -f "pattern"
trap 'cleanup' EXIT
trap 'echo "Interrupted"' INT
cleanup() {
rm -f "$TEMP_FILE"
exit 0
}
3. Background Jobs
command &
nohup command &
nohup cmd > log.txt 2>&1 &
jobs
fg %1
bg %1
disown
4. Cron Scheduling
0 * * * *
*/15 * * * *
0 0 * * *
0 0 * * 0
crontab -e
crontab -l
Common Patterns
Daemon Pattern
start_daemon() {
nohup ./daemon.sh >> /var/log/daemon.log 2>&1 &
echo "$!" > /var/run/daemon.pid
disown
}
stop_daemon() {
if [[ -f /var/run/daemon.pid ]]; then
kill "$(cat /var/run/daemon.pid)"
rm /var/run/daemon.pid
fi
}
Cron with Locking
0 * * * * /usr/bin/flock -n /var/lock/job.lock /path/to/script.sh
Signal Handler
#!/usr/bin/env bash
set -euo pipefail
cleanup() {
echo "Cleaning up..."
rm -f "$TEMP_FILE"
}
trap cleanup EXIT INT TERM
TEMP_FILE=$(mktemp)
Signal Reference
| Signal | Number | Default | Common Use |
|---|
| SIGHUP | 1 | Terminate | Reload config |
| SIGINT | 2 | Terminate | Ctrl+C |
| SIGQUIT | 3 | Core dump | Ctrl+\ |
| SIGKILL | 9 | Terminate | Force kill |
| SIGTERM | 15 | Terminate | Graceful stop |
| SIGSTOP | 19 | Stop | Pause |
| SIGCONT | 18 | Continue | Resume |
Anti-Patterns
| Don't | Do | Why |
|---|
kill -9 first | kill -TERM first | Allow cleanup |
| Kill PID 1 | Never | Crashes system |
| Cron without logs | Log all output | Debug issues |
Practice Exercises
- Process Monitor: Script to monitor a process
- Daemon Script: Create a proper daemon
- Cron Job: Schedule a backup job
- Signal Handler: Graceful shutdown script
Troubleshooting
Common Errors
| Error | Cause | Fix |
|---|
No such process | Already dead | Check with ps |
Operation not permitted | Wrong owner | Use sudo |
| Cron not running | PATH issues | Use full paths |
Debug Techniques
ps -p $PID
grep CRON /var/log/syslog
strace -p $PID
Resources