Process Management

Learn to manage processes in Bash: view, start, stop, and monitor processes. Master job control, background processes, signals, and system monitoring.

📖 4 min read📅 2026-02-10Scripting

Viewing Processes

ps — Process Status

# Current shell's processes
ps
 
# All processes (BSD style)
ps aux
 
# All processes (Unix style)
ps -ef
 
# Process tree
ps auxf
# Or:
pstree
 
# Specific process
ps aux | grep nginx
# Avoid grep showing itself:
ps aux | grep [n]ginx
 
# Custom format
ps -eo pid,ppid,user,%cpu,%mem,cmd --sort=-%cpu | head -20

top / htop — Real-Time Monitoring

# top - built-in process monitor
top
 
# htop - improved process monitor (install if needed)
sudo apt install htop
htop
 
# top shortcuts:
# P = sort by CPU
# M = sort by memory
# k = kill process
# q = quit
# 1 = show per-CPU stats

Starting Processes

Foreground and Background

# Run in foreground (default)
./long_script.sh
 
# Run in background
./long_script.sh &
 
# Run and get PID
./script.sh &
echo "PID: $!"
 
# Prevent hangup when terminal closes
nohup ./long_script.sh &
nohup ./script.sh > output.log 2>&1 &
 
# Disown a background process
./script.sh &
disown

Job Control

# List background jobs
jobs
jobs -l    # With PIDs
 
# Suspend foreground process
# Press Ctrl+Z
 
# Resume in background
bg %1        # Job 1 in background
 
# Bring to foreground
fg %1        # Job 1 to foreground
 
# Kill a job
kill %1
 
# Example workflow
./script1.sh &    # Start in background (Job 1)
./script2.sh      # Start in foreground
# Ctrl+Z           # Suspend script2 (Job 2)
bg %2             # Resume script2 in background
jobs              # See both running
fg %1             # Bring script1 to foreground

Signals

# Common signals
kill -l           # List all signals
 
# Important signals:
# SIGTERM (15) - Graceful termination (default)
# SIGKILL (9)  - Force kill (cannot be caught)
# SIGINT (2)   - Interrupt (Ctrl+C)
# SIGHUP (1)   - Hangup (terminal closed)
# SIGSTOP (19) - Pause (cannot be caught)
# SIGCONT (18) - Continue after stop
 
# Kill by PID
kill 1234          # Send SIGTERM
kill -9 1234       # Force kill (SIGKILL)
kill -SIGTERM 1234 # Same as kill 1234
 
# Kill by name
killall nginx
pkill -f "python script.py"
 
# Kill all processes of a user
killall -u username

Trapping Signals in Scripts

#!/bin/bash
# Cleanup on exit
 
cleanup() {
    echo "Cleaning up temporary files..."
    rm -f /tmp/myapp_*.tmp
    echo "Done."
    exit 0
}
 
# Trap signals
trap cleanup EXIT SIGINT SIGTERM
 
echo "Running... (Press Ctrl+C to stop)"
echo "PID: $$"
 
# Create temp files
touch /tmp/myapp_$$.tmp
 
# Main work
while true; do
    echo "Working..."
    sleep 5
done

System Monitoring

Resource Usage

# Memory usage
free -h
 
# Disk usage
df -h
 
# IO statistics
iostat
 
# Network connections
ss -tuln          # Listening ports
ss -tunap         # All connections with PIDs
netstat -tuln     # Alternative (older)
 
# System uptime and load
uptime
w                 # Who's logged in + uptime

Monitoring Scripts

#!/bin/bash
# Simple system monitor
 
while true; do
    clear
    echo "=== System Monitor ($(date)) ==="
    echo
 
    echo "--- CPU Load ---"
    uptime | awk -F'load average:' '{print $2}'
 
    echo
    echo "--- Memory ---"
    free -h | awk 'NR==2{printf "Used: %s / %s (%.1f%%)\n", $3, $2, $3/$2*100}'
 
    echo
    echo "--- Disk ---"
    df -h / | awk 'NR==2{printf "Used: %s / %s (%s)\n", $3, $2, $5}'
 
    echo
    echo "--- Top 5 CPU Processes ---"
    ps aux --sort=-%cpu | awk 'NR<=6{printf "%-8s %-6s %s\n", $1, $3"%", $11}'
 
    sleep 5
done

Cron Jobs (Scheduled Tasks)

# Edit crontab
crontab -e
 
# List cron jobs
crontab -l
 
# Cron format:
# MIN HOUR DAY MONTH WEEKDAY COMMAND
# *   *    *   *     *
# 0-59 0-23 1-31 1-12 0-7(0,7=Sun)
 
# Examples:
# Every minute
* * * * * /path/to/script.sh
 
# Every day at 2:30 AM
30 2 * * * /path/to/backup.sh
 
# Every Monday at 9 AM
0 9 * * 1 /path/to/report.sh
 
# Every 15 minutes
*/15 * * * * /path/to/check.sh
 
# First day of every month
0 0 1 * * /path/to/monthly.sh
 
# Redirect output to log
0 2 * * * /path/to/script.sh >> /var/log/myscript.log 2>&1

Exercises

  1. Write a script that monitors a process and restarts it if it crashes
  2. Create a system health check that runs via cron every 5 minutes
  3. Write a script that gracefully stops all instances of a given application
  4. Build a process monitor that alerts when CPU/memory exceeds thresholds
  5. Create a cleanup script that runs on shutdown using trap

Next: Advanced Bash Scripting — error handling, debugging, and best practices!