What is a Process?
Every command you run creates a process — an instance of a running program with its own PID (Process ID).
| Type | Description | Example |
|---|---|---|
| Foreground | Blocks terminal until complete | apt update |
| Background | Runs 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 auxftop — Real-Time Monitor
topKey columns:
- PID — Process ID
- %CPU — CPU usage
- %MEM — Memory usage
- COMMAND — Program name
Interactive keys in top:
q— Quitk— Kill a process (enter PID)M— Sort by memoryP— Sort by CPU
htop — Enhanced Monitor
# Install if needed
sudo apt install htop -y
htophtop 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 immediatelyPausing 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
jobsFinding 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| Signal | Number | Behavior |
|---|---|---|
| SIGTERM | 15 | Graceful shutdown (default) |
| SIGKILL | 9 | Immediate forced termination |
| SIGHUP | 1 | Reload configuration |
| SIGSTOP | 19 | Pause 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.shManaging Crontab
# Edit your crontab
crontab -e
# List your cron jobs
crontab -l
# Remove all cron jobs
crontab -rPractical 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.logSchedule 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
| Expression | Equivalent |
|---|---|
@reboot | Run once at startup |
@hourly | 0 * * * * |
@daily | 0 0 * * * |
@weekly | 0 0 * * 0 |
@monthly | 0 0 1 * * |
# Run backup script at every reboot
@reboot /home/ubuntu/startup_tasks.shSummary
- Every running program is a process with a unique PID
ps auxlists processes,top/htopmonitor in real-time&runs commands in background,Ctrl+Zpauses,bg/fgresumeskill PIDterminates gracefully,kill -9 PIDforces 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.