Advanced โฑ 25 min Lesson 12 of 13

โฐ Process Management & Automation

Background processes, job control, cron jobs, and script arguments.

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

bash
# 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).

bash
# 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)
๐Ÿ’ก Tip: Always try 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.

bash
# 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.sh

One-Time Scheduling with at

bash
# 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 5

Script Arguments with getopts

getopts parses command-line options for your scripts, making them behave like professional CLI tools.

bash
#!/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"
โš ๏ธ Warning: Always redirect cron job output to a log file, e.g., * * * * * /script.sh >> /var/log/myscript.log 2>&1. Otherwise, errors vanish into the void.

Try It Yourself

Terminal

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.

๐Ÿงช Test Your Knowledge

Answer the questions below to check your understanding of this lesson.