Bash Process Management - ps, kill, top, bg, fg, jobs, nohup
Manage running processes in bash with ps for listing, kill for signaling, top/htop for monitoring, and job control commands for background tasks.
Detailed Explanation
Process Management in Bash
Understanding how to manage processes is critical for system administration and development. Bash provides tools for listing, monitoring, signaling, and controlling processes.
Viewing Processes with ps
The ps command shows information about running processes:
ps aux # all processes with details
ps aux | grep nginx # find specific process
ps aux --sort=-%mem | head -10 # top 10 by memory usage
ps -ef --forest # process tree
ps -u www-data # processes for specific user
Killing Processes
The kill command sends signals to processes:
kill PID # SIGTERM (graceful shutdown, default)
kill -9 PID # SIGKILL (force kill, cannot be caught)
kill -1 PID # SIGHUP (reload configuration)
killall node # kill all processes by name
pkill -f "python app.py" # kill by command pattern
Monitoring with top and htop
top provides a real-time view of system resource usage:
top # interactive process viewer
top -bn 1 | head -20 # one snapshot for scripting
htop # enhanced version (if installed)
Background Jobs
Bash supports running processes in the background:
# Start in background
long_task &
# Suspend and background
# Press Ctrl+Z, then:
bg
# List background jobs
jobs
# Bring to foreground
fg %1
Keeping Processes Running
nohup prevents a process from being terminated when the terminal closes:
nohup ./server.sh > output.log 2>&1 &
disown # remove from shell's job table
Waiting for Processes
# Wait for all background jobs
wait
# Wait for specific PID
wait $PID
# Run commands in parallel and wait
task1 &
task2 &
task3 &
wait
echo "All tasks completed"
Process Substitution for Monitoring
# Watch a command repeatedly
watch -n 2 'ps aux | grep node'
# Monitor specific process
while kill -0 $PID 2>/dev/null; do
echo "Process $PID still running..."
sleep 5
done
echo "Process $PID has exited"
Use Case
Process management skills are essential for server administration, debugging production issues, managing long-running tasks, and building deployment scripts. System administrators use these commands daily to monitor server health, restart services, and troubleshoot resource usage. Developers use them to manage development servers and background tasks.
Try It — Bash Cheat Sheet
Related Topics
Bash File Operations - ls, cp, mv, rm, find, chmod
File Operations
Bash Pipes and Redirects - |, >, >>, 2>, <, <<
Pipes & Redirects
Bash Script Basics - Shebang, Arguments, Exit Codes, set Options
Script Basics
Bash Error Handling - set -e, trap, Exit Codes, Retry Patterns
Error Handling
Bash System Info Commands - df, du, free, uname, uptime, lsblk
System Info