Controlling Processes
Linux is a multi-tasking system. Understanding how to manage processes โ starting them in the background, monitoring them, and automating their execution โ is a critical skill for any sysadmin or developer.
Background Processes & Job Control
# Run a command in the background with &
long_task &
# List background jobs
jobs
# Bring a background job to the foreground
fg %1 # job number 1
# Send a foreground process to the background
# 1. Press Ctrl+Z to suspend it
# 2. Then type:
bg %1
# Wait for background processes to finish
wait # wait for all
wait $PID # wait for a specific process
# Get PID of last background process
long_task &
echo "PID is $!"Signals & Killing Processes
Processes can receive signals โ notifications that tell them to do something (usually stop).
# Common signals
# SIGTERM (15) โ polite "please exit" (default for kill)
# SIGKILL (9) โ forceful kill (cannot be caught)
# SIGINT (2) โ interrupt (Ctrl+C)
# SIGHUP (1) โ hangup (terminal closed)
# Kill a process by PID
kill 12345 # sends SIGTERM
kill -9 12345 # sends SIGKILL (force)
# Kill by name
pkill firefox
killall firefox
# Find processes
ps aux | grep nginx
pgrep -l nginx
# Top โ interactive process monitor
top
htop # enhanced version (if installed)kill (SIGTERM) before kill -9 (SIGKILL). SIGTERM gives the process a chance to clean up. SIGKILL is a last resort.
Cron โ Scheduled Tasks
cron is a time-based job scheduler. You define tasks in a crontab file.
# Edit your crontab
crontab -e
# List your crontab
crontab -l
# Crontab format:
# โโโโโโโโโโโโโโ minute (0 - 59)
# โ โโโโโโโโโโโโโโ hour (0 - 23)
# โ โ โโโโโโโโโโโโโโ day of month (1 - 31)
# โ โ โ โโโโโโโโโโโโโโ month (1 - 12)
# โ โ โ โ โโโโโโโโโโโโโโ day of week (0 - 7, 0 or 7 = Sunday)
# โ โ โ โ โ
# * * * * * command_to_run
# Examples:
# Every day at 2:30 AM
30 2 * * * /home/user/backup.sh
# Every 15 minutes
*/15 * * * * /home/user/check_health.sh
# Every Monday at 9 AM
0 9 * * 1 /home/user/weekly_report.sh
# First day of every month at midnight
0 0 1 * * /home/user/monthly_cleanup.shOne-Time Scheduling with at
# Schedule a one-time task
echo "/home/user/backup.sh" | at 2:30 AM tomorrow
# Schedule relative to now
echo "echo done" | at now + 30 minutes
# List pending at jobs
atq
# Remove an at job
atrm 5Script Arguments with getopts
getopts parses command-line options for your scripts, making them behave like professional CLI tools.
#!/bin/bash
usage() {
echo "Usage: $0 [-v] [-o output] [-n count] file"
exit 1
}
verbose=0
output=""
count=1
while getopts "vo:n:h" opt; do
case $opt in
v) verbose=1 ;;
o) output="$OPTARG" ;;
n) count="$OPTARG" ;;
h) usage ;;
?) usage ;;
esac
done
# Shift past the parsed options
shift $((OPTIND - 1))
# Remaining arguments
file="${1:?Missing filename}"
[[ $verbose -eq 1 ]] && echo "Processing $file..."
echo "Output: ${output:-stdout}, Count: $count"* * * * * /script.sh >> /var/log/myscript.log 2>&1. Otherwise, errors vanish into the void.
Try It Yourself
Summary
You've learned how to run background processes, manage jobs, send signals with kill, schedule recurring tasks with cron, one-time tasks with at, and parse command-line arguments with getopts. These are the tools that turn scripts into automated workflows.