Processes & Crontab

25 minLesson 7 of 16

Learning Objectives

  • Understand foreground and background processes
  • Monitor processes with ps, top, and htop
  • Kill and manage running processes
  • Schedule recurring tasks with crontab

What is a Process?

Every command you run creates a process — an instance of a running program with its own PID (Process ID).

TypeDescriptionExample
ForegroundBlocks terminal until completeapt update
BackgroundRuns without blocking terminal./backup.sh &

Viewing Processes

ps — Process Status

# Your processes
ps
 
# All processes with details
ps aux
 
# Find a specific process
ps aux | grep nginx
 
# Tree view (shows parent-child relationships)
ps auxf

top — Real-Time Monitor

top

Key columns:

  • PID — Process ID
  • %CPU — CPU usage
  • %MEM — Memory usage
  • COMMAND — Program name

Interactive keys in top:

  • q — Quit
  • k — Kill a process (enter PID)
  • M — Sort by memory
  • P — Sort by CPU

htop — Enhanced Monitor

# Install if needed
sudo apt install htop -y
 
htop

htop provides a colorful, scrollable interface with mouse support.

Managing Processes

Running in Background

# Start a process in the background
./long_script.sh &
 
# The & sends it to background immediately

Pausing and Resuming

# Run a command
ping google.com
 
# Press Ctrl+Z to pause (suspend)
# [1]+ Stopped  ping google.com
 
# Resume in background
bg
 
# Resume in foreground
fg
 
# List background jobs
jobs

Finding a Process PID

# By name
pidof nginx
 
# Using pgrep
pgrep -f "python3 app.py"

Killing Processes

# Graceful termination (SIGTERM)
kill PID
 
# Force kill (SIGKILL) — use as last resort
kill -9 PID
 
# Kill by name
pkill nginx
 
# Kill all instances
killall python3
SignalNumberBehavior
SIGTERM15Graceful shutdown (default)
SIGKILL9Immediate forced termination
SIGHUP1Reload configuration
SIGSTOP19Pause process

Crontab — Scheduling Tasks

Cron automatically executes commands on a schedule. Essential for:

  • Automated backups
  • Log rotation
  • Health checks
  • Data synchronization

Cron Syntax

┌───────── minute (0-59)
│ ┌─────── hour (0-23)
│ │ ┌───── day of month (1-31)
│ │ │ ┌─── month (1-12)
│ │ │ │ ┌─ day of week (0-6, Sun=0)
│ │ │ │ │
* * * * * command

Examples

# Every day at 2:30 AM
30 2 * * * /home/ubuntu/backup.sh
 
# Every hour
0 * * * * /home/ubuntu/health_check.sh
 
# Every Monday at 9 AM
0 9 * * 1 /home/ubuntu/weekly_report.sh
 
# Every 5 minutes
*/5 * * * * /home/ubuntu/monitor.sh
 
# First day of every month at midnight
0 0 1 * * /home/ubuntu/monthly_cleanup.sh

Managing Crontab

# Edit your crontab
crontab -e
 
# List your cron jobs
crontab -l
 
# Remove all cron jobs
crontab -r

Practical Example

Create a monitoring script:

#!/bin/bash
# /home/ubuntu/monitor.sh
echo "$(date) - Disk: $(df -h / | awk 'NR==2{print $5}') | Mem: $(free -m | awk 'NR==2{print $3}')MB used" >> /home/ubuntu/system.log

Schedule it:

chmod +x /home/ubuntu/monitor.sh
crontab -e
# Add: */10 * * * * /home/ubuntu/monitor.sh
💡

Always use absolute paths in crontab. Cron runs with a minimal environment — relative paths and aliases won't work.

Special Cron Expressions

ExpressionEquivalent
@rebootRun once at startup
@hourly0 * * * *
@daily0 0 * * *
@weekly0 0 * * 0
@monthly0 0 1 * *
# Run backup script at every reboot
@reboot /home/ubuntu/startup_tasks.sh

Summary

  • Every running program is a process with a unique PID
  • ps aux lists processes, top/htop monitor in real-time
  • & runs commands in background, Ctrl+Z pauses, bg/fg resumes
  • kill PID terminates gracefully, kill -9 PID forces termination
  • Crontab schedules recurring tasks with * * * * * syntax
  • Always use absolute paths in cron jobs

Next Steps

You now have a solid Linux and Bash foundation. The next module covers system administration — memory management, storage, networking, and server configuration.