wEEK 13
Time Management and Task Scheduling
This week focuses on how Linux automates repetitive tasks through cron, the built-in job scheduler. Cron allows the system to run commands at precise times—every minute, every night, every Monday, or on any recurring pattern you define.
Most background maintenance on a Linux system—log rotation, backups, system updates—runs through cron. Understanding it gives you control over when and how your system works without you manually intervening.
We follow the four-step C-R-O-N framework:
C — Create Cron Jobs
A cron job is an entry in a schedule list called crontab. Each user (including root) can maintain their own crontab file.
Cron jobs run in the background, so they must be written clearly and tested carefully.
| Command | Purpose | Description |
|---|---|---|
crontab -l | List current cron jobs | Shows all jobs scheduled by the current user. |
crontab -e | Edit jobs | Opens your crontab in a text editor. Add/change scheduled tasks here. |
crontab -r | Remove all jobs | Deletes the current user’s entire cron schedule. Use carefully. |
A cron entry uses the format:
* * * * * command-to-run
| | | | |
| | | | └── Day of week (0–6, Sunday=0)
| | | └──── Month (1–12)
| | └────── Day of month (1–31)
| └──────── Hour (0–23)
└────────── Minute (0–59)
Example: Run a script every day at 2:30 AM: 30 2 * * * /home/user/backup.sh
R — Recurring Patterns
Cron excels at repetition. You can choose patterns that define exactly how often a job runs.
| Pattern | Example | Meaning |
|---|---|---|
* * * * * | Every minute | Run constantly—use sparingly. |
*/5 * * * * | Every 5 minutes | Spaced recurring jobs. |
0 * * * * | At minute 0, every hour | Hourly tasks. |
0 3 * * 1 | 3 AM every Monday | Weekly scheduled jobs. |
@daily | Midnight daily | Named shortcut schedules. |
@reboot | On startup | Runs once at boot time. |
Example: Run a Python script every 15 minutes: */15 * * * * /usr/bin/python3 /home/user/collect_metrics.py
Recurring schedules let the system work for you—automatically.
O — Observe Schedules
You should verify that cron is running and that your jobs are recognized.
| Command | Purpose | Description |
|---|---|---|
systemctl status cron | Check cron service | Confirms that the scheduler is active. |
grep CRON /var/log/syslog | View cron runtime logs | Shows when jobs run and whether they succeeded. |
tail -f /var/log/syslog | Watch events live | Useful for debugging while a job executes. |
Cron does not display errors directly to your terminal. Logs are essential for understanding what the job actually did.
Navigate the tree
N — Note Results
Always record outcomes—especially for backups, processing tasks, and data maintenance.
| Command | Purpose | Description |
|---|---|---|
echo "log info" >> ~/cron.log | Store output | Append messages to a personal log file. |
command >> output.log 2>&1 | Log both output and errors | Ensures you capture everything. |
mail (if configured) | Receive cron output emails | Cron can email job results automatically. |
Example cron entry that logs results: 0 1 * * * /usr/local/bin/rotate_data.sh >> /home/user/cron.log 2>&1
Without logging, cron may run quietly and fail quietly. Always leave a trail.
This concludes Lecture 13 on Time Management and Task Scheduling. Please return to Blackboard to access the Week 13 materials.