Process
Chapter 23: Process Management in Bash
Section titled “Chapter 23: Process Management in Bash”Overview
Section titled “Overview”Process management is a critical skill for DevOps engineers and system administrators. This chapter covers how to monitor, control, and manage processes in Linux using bash scripts.
Understanding Processes
Section titled “Understanding Processes”What is a Process?
Section titled “What is a Process?”A process is an instance of a running program. Each process has:
- PID (Process ID) - Unique identifier
- PPID (Parent Process ID) - Parent process
- UID/GID - User and group IDs
- Status - Running, sleeping, stopped, zombie
- Resources - Memory, CPU, file descriptors
Process States
Section titled “Process States”┌────────────────────────────────────────────────────────────────┐│ Process States │├────────────────────────────────────────────────────────────────┤│ ││ ┌─────┐ ││ │ New │ Process being created ││ └──┬──┘ ││ │ ││ ▼ ││ ┌──────────┐ ┌─────────────┐ ││ │ Running │ ──► │ Terminated │ ││ │ (R) │ │ (Z) │ ││ └────┬─────┘ └─────────────┘ ││ │ ││ ┌────┴─────┐ ││ │ │ ││ ▼ ▼ ││ ┌────┐ ┌────┐ ││ │Sleep│ │Stop│ ││ │(S) │ │(T) │ ││ └────┘ └────┘ ││ ││ R - Running/Runnable ││ S - Interruptible Sleep (waiting for event) ││ D - Uninterruptible Sleep (usually I/O) ││ T - Stopped (by signal) ││ Z - Zombie (terminated but not reaped) ││ │└────────────────────────────────────────────────────────────────┘Viewing Processes
Section titled “Viewing Processes”ps Command
Section titled “ps Command”# Basic ps outputps
# Full formatps -efps -aux
# Show threadsps -eLfps -eT
# Custom outputps -eo pid,ppid,cmd,%mem,%cpu
# Sort by memoryps aux --sort=-%mem
# Sort by CPUps aux --sort=-%cpu
# Show process treeps -ef --forest
# Show user's processesps -U usernametop Command
Section titled “top Command”# Interactive process viewertop
# Batch mode (useful for scripts)top -bn1
# Top with specific refreshtop -d 5 -n 3
# Show only specific usertop -u username
# Show specific processtop -p $(pgrep -f process_name)
# Custom fieldstop -o %MEMhtop (Enhanced top)
Section titled “htop (Enhanced top)”# Interactive process manager (install: sudo pacman -S htop)htop
# Show specific columnshtop -d 10
# Filter processeshtop -F nginxOther Process Viewers
Section titled “Other Process Viewers”# Continuous process monitoringwatch -n 1 'ps aux | grep nginx'
# Tree viewpstree
# Find process by namepgrep -a nginx
# Find process with full detailspidof nginxProcess Information Commands
Section titled “Process Information Commands”/proc Filesystem
Section titled “/proc Filesystem”# Process statuscat /proc/$PID/status
# Process command linecat /proc/$PID/cmdline
# Process environmentcat /proc/$PID/environ
# Process file descriptorsls -la /proc/$PID/fd
# Process limitscat /proc/$PID/limitsStarting Processes
Section titled “Starting Processes”Running in Foreground
Section titled “Running in Foreground”# Standard foreground processnginx
# With output redirection./script.sh > output.log 2>&1 &Running in Background
Section titled “Running in Background”# Start process in background./long_running_script.sh &
# Start with nohup (immune to hangup)nohup ./script.sh &
# Start with redirectnohup ./script.sh > output.log 2>&1 &
# Using setsid (new session)setsid ./script.sh &
# Using disown (remove from shell)./script.sh &disownProcess Control Commands
Section titled “Process Control Commands”kill - Send Signals
Section titled “kill - Send Signals”# Kill by PIDkill 1234
# Kill by process namepkill nginx
# Kill all processes by namekillall nginx
# Kill with specific signalkill -TERM 1234 # Graceful termination (15)kill -KILL 1234 # Force kill (9)kill -HUP 1234 # Reload configuration (1)kill -INT 1234 # Interrupt (2)
# Common signalskill -SIGHUP 1234 # Reloadkill -SIGTERM 1234 # Graceful stopkill -SIGKILL 1234 # Force stopkill -SIGSTOP 1234 # Stop processkill -SIGCONT 1234 # Continue stopped processkillall - Kill by Name
Section titled “killall - Kill by Name”# Kill all processes by namekillall nginx
# Kill by exact namekillall -exact nginx
# Kill processes owned by userkillall -u username nginx
# Interactivekillall -i nginxpkill - Kill by Pattern
Section titled “pkill - Kill by Pattern”# Kill by namepkill nginx
# Kill by patternpkill -f "python.*script"
# Kill by userpkill -u username
# Kill with signalpkill -9 -f patternJob Control
Section titled “Job Control”Background and Foreground
Section titled “Background and Foreground”# Start in background./script.sh &
# List jobsjobs
# Bring to foregroundfg %1
# Send to background (from fg)Ctrl+Zbg
# Bring specific jobfg %2Managing Multiple Processes
Section titled “Managing Multiple Processes”Wait for Process
Section titled “Wait for Process”# Wait for specific PIDwait $PID
# Wait for background jobswait
# Wait with timeouttimeout 30 ./script.shProcess Substitution in Loops
Section titled “Process Substitution in Loops”# Run commands in parallelfor file in *.txt; do process "$file" &donewait
# Parallel processing with limitsem -j 4 ./process.shProcess Monitoring Scripts
Section titled “Process Monitoring Scripts”Monitor Process Existence
Section titled “Monitor Process Existence”#!/usr/bin/env bash# Check if process is running
PROCESS_NAME="${1:-nginx}"
if pgrep -x "$PROCESS_NAME" > /dev/null; then echo "$PROCESS_NAME is running" exit 0else echo "$PROCESS_NAME is not running" exit 1fiProcess Monitor with Restart
Section titled “Process Monitor with Restart”#!/usr/bin/env bash# Monitor and restart process
PROCESS_NAME="${1:-nginx}"RESTART_DELAY=5
while true; do if pgrep -x "$PROCESS_NAME" > /dev/null; then echo "$(date): $PROCESS_NAME is running" else echo "$(date): $PROCESS_NAME stopped, restarting..." $PROCESS_NAME & fi sleep $RESTART_DELAYdoneResource Usage Monitor
Section titled “Resource Usage Monitor”#!/usr/bin/env bash# Monitor CPU and memory usage
PID="${1:-$$}"
while true; do if [[ -d "/proc/$PID" ]]; then # Get CPU and memory cpu=$(ps -p $PID -o %cpu=) mem=$(ps -p $PID -o %mem=)
echo "$(date): CPU=$cpu% MEM=$mem%" else echo "Process $PID not found" exit 1 fi sleep 5doneReal-World DevOps Examples
Section titled “Real-World DevOps Examples”Docker Process Management
Section titled “Docker Process Management”#!/usr/bin/env bash# Manage Docker containers
CONTAINER_NAME="myapp"
# Check if runningif docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then echo "Container is running"else echo "Container not running, starting..." docker start $CONTAINER_NAMEfi
# Restart with health checkrestart_container() { local container="$1" docker restart "$container"
# Wait for health check local max_attempts=30 local attempt=0
while [ $attempt -lt $max_attempts ]; do if docker inspect --format='{{.State.Health.Status}}' "$container" 2>/dev/null | grep -q "healthy"; then echo "Container healthy" return 0 fi sleep 2 ((attempt++)) done
echo "Health check failed" return 1}Kubernetes Process Management
Section titled “Kubernetes Process Management”#!/usr/bin/env bash# Manage Kubernetes pods
NAMESPACE="${1:-default}"
# Get pod statuskubectl get pods -n "$NAMESPACE" -o wide
# Watch pod statuskubectl get pods -n "$NAMESPACE" --watch
# Get process info in podkubectl exec -it pod-name -n "$NAMESPACE" -- ps aux
# Debug pod issueskubectl debug pod-name -n "$NAMESPACE" --image=busybox -- shSystemd Service Management
Section titled “Systemd Service Management”#!/usr/bin/env bash# Manage systemd services
SERVICE_NAME="nginx.service"
# Check statussystemctl status $SERVICE_NAME
# Start/Stop/Restartsystemctl start $SERVICE_NAMEsystemctl stop $SERVICE_NAMEsystemctl restart $SERVICE_NAME
# Enable/Disable at bootsystemctl enable $SERVICE_NAMEsystemctl disable $SERVICE_NAME
# View logsjournalctl -u $SERVICE_NAME -f
# Restart on failure (systemd)# Add to service file:# Restart=on-failure# RestartSec=5Process CPU Affinity
Section titled “Process CPU Affinity”#!/usr/bin/env bash# Set CPU affinity
PID="$1"CPU_LIST="0,1,2,3"
# Set CPU affinitytaskset -cp $CPU_LIST $PID
# View CPU affinitytaskset -p $PID
# Run on specific CPUtaskset -c 0 ./process.shProcess Priority (Nice/ionice)
Section titled “Process Priority (Nice/ionice)”#!/usr/bin/env bash# Set process priority
# Nice value (lower = higher priority, -20 to 19)nice -n 10 ./script.sh # Lower prioritynice -n -5 ./script.sh # Higher priority (requires root)
# I/O priority (best-effort, idle, real-time)ionice -c 3 -p $PID # Idle priorityionice -c 2 -n 7 -p $PID # Best-effort, lowest priority
# Run with bothnice -n 10 ionice -c 3 ./script.shProcess Limits
Section titled “Process Limits”ulimit
Section titled “ulimit”# View all limitsulimit -a
# Set max processesulimit -u 4096
# Set max file descriptorsulimit -n 8192
# Set max memory sizeulimit -v unlimited
# Persistent limits (add to /etc/security/limits.conf)# username soft nofile 8192# username hard nofile 16384Zombie and Orphan Processes
Section titled “Zombie and Orphan Processes”Finding Zombies
Section titled “Finding Zombies”# Find zombie processesps aux | grep 'Z'
# More detailedps -eo pid,ppid,state,cmd | grep Z
# System-widecat /proc/*/stat | awk '$3=="Z" {print $1}'Killing Zombie Processes
Section titled “Killing Zombie Processes”# Zombies are already dead, need to kill parentkill -9 $(ps -eo pid,ppid,state,cmd | grep Z | awk '{print $2}')
# Or restart parentsystemctl restart parent_serviceAdvanced Process Operations
Section titled “Advanced Process Operations”strace - Trace System Calls
Section titled “strace - Trace System Calls”# Trace processstrace -p $PID
# Trace and save to filestrace -o output.txt -p $PID
# Trace specific system callsstrace -e trace=open,read,write -p $PID
# Follow child processesstrace -f -p $PIDltrace - Trace Library Calls
Section titled “ltrace - Trace Library Calls”# Trace library callsltrace -p $PID
# Output to fileltrace -o output.txt -p $PIDSummary
Section titled “Summary”In this chapter, you learned:
- ✅ Understanding Linux processes and states
- ✅ Viewing processes (ps, top, htop)
- ✅ /proc filesystem
- ✅ Starting processes (foreground/background)
- ✅ Process control (kill, killall, pkill)
- ✅ Job control
- ✅ Process monitoring scripts
- ✅ Docker/Kubernetes process management
- ✅ Systemd service management
- ✅ Process priority and limits
- ✅ Finding and handling zombie processes
- ✅ System call tracing
Next Steps
Section titled “Next Steps”Continue to the next chapter to learn about Signals and Traps.
Previous Chapter: awk - Pattern Scanning Next Chapter: Signals and Traps