- What is Cron?
- Crontab Syntax
- Managing Crontabs
- Practical Examples
- System Cron Directories
- The
atCommand β One-Time Tasks - Systemd Timers
- Anacron β For Desktops/Laptops
- Practice Exercises
Cron is a time-based job scheduler. It runs commands or scripts at specified intervals.
π Analogy: Cron is like setting an alarm clock β but instead of waking you up, it runs a command.
ββββββββββββββ minute (0-59)
β ββββββββββββββ hour (0-23)
β β ββββββββββββββ day of month (1-31)
β β β ββββββββββββββ month (1-12)
β β β β ββββββββββββββ day of week (0-7, 0 and 7 = Sunday)
β β β β β
* * * * * command_to_execute
| Character | Meaning | Example |
|---|---|---|
* |
Every value | * * * * * = every minute |
, |
Multiple values | 1,15,30 = at 1, 15, 30 |
- |
Range | 1-5 = 1 through 5 |
/ |
Step | */15 = every 15 |
@reboot |
Run once at startup | @reboot /script.sh |
| Schedule | Cron Expression | Description |
|---|---|---|
| Every minute | * * * * * |
Runs every minute |
| Every hour | 0 * * * * |
At minute 0 of every hour |
| Every day at midnight | 0 0 * * * |
12:00 AM daily |
| Every day at 6 AM | 0 6 * * * |
6:00 AM daily |
| Every Monday | 0 0 * * 1 |
Monday at midnight |
| Every weekday | 0 9 * * 1-5 |
Mon-Fri at 9 AM |
| Every 15 minutes | */15 * * * * |
:00, :15, :30, :45 |
| First of month | 0 0 1 * * |
Midnight, 1st day |
| Every 6 hours | 0 */6 * * * |
00:00, 06:00, 12:00, 18:00 |
| Shortcut | Equivalent |
|---|---|
@yearly |
0 0 1 1 * |
@monthly |
0 0 1 * * |
@weekly |
0 0 * * 0 |
@daily |
0 0 * * * |
@hourly |
0 * * * * |
@reboot |
At system startup |
crontab -e # Edit your crontab
crontab -l # List your crontab
crontab -r # Remove your crontab (careful!)
crontab -u alice -l # List alice's crontab (root only)
sudo crontab -e # Edit root's crontab# Edit crontab
crontab -e
# Backup home directory every day at 2 AM
0 2 * * * tar -czf /backup/home-$(date +\%Y\%m\%d).tar.gz /home/sovon
# Check disk space every hour, alert if > 90%
0 * * * * df -h | awk '$5 > 90 {print}' | mail -s "Disk Alert" admin@example.com
# Rotate logs every Sunday at 3 AM
0 3 * * 0 /usr/sbin/logrotate /etc/logrotate.conf
# Update packages every day at 4 AM
0 4 * * * apt update && apt upgrade -y >> /var/log/auto-update.log 2>&1
# Clean /tmp every day at midnight
0 0 * * * find /tmp -type f -mtime +7 -delete
# Send a report every Friday at 5 PM
0 17 * * 5 /home/sovon/scripts/weekly-report.sh
# Run at reboot
@reboot /home/sovon/scripts/startup.sh# Log stdout and stderr to file
0 * * * * /script.sh >> /var/log/myjob.log 2>&1
# Discard all output
0 * * * * /script.sh > /dev/null 2>&1
# Send output as email (if mail is configured)
MAILTO="sovon@example.com"
0 6 * * * /script.sh# Cron runs with a minimal environment! Set paths explicitly:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
SHELL=/bin/bash
HOME=/home/sovon
MAILTO=""
# Or use full paths in your commands
0 * * * * /usr/bin/python3 /home/sovon/scripts/myscript.py
β οΈ Common Gotcha: Cron doesn't load your.bashrc. Commands may fail because thePATHis different. Always use full paths!
# Scripts placed here run automatically:
/etc/cron.d/ # Custom cron files
/etc/cron.hourly/ # Run every hour
/etc/cron.daily/ # Run every day
/etc/cron.weekly/ # Run every week
/etc/cron.monthly/ # Run every month
# Just drop an executable script in the directory:
sudo cp my-backup.sh /etc/cron.daily/
sudo chmod +x /etc/cron.daily/my-backup.sh
# System-wide crontab
cat /etc/crontabat schedules a command to run once at a specific time.
sudo apt install at
# Schedule for a specific time
at 10:00 PM
> /home/sovon/scripts/cleanup.sh
> <Ctrl+D>
# Schedule relative to now
at now + 30 minutes
> echo "Reminder: meeting!" | mail -s "Meeting" sovon
> <Ctrl+D>
# Schedule for a specific date
at 2:00 PM Feb 25
> /backup/run.sh
> <Ctrl+D>
# List pending jobs
atq
# Remove a job
atrm 3 # Remove job #3
# View job details
at -c 3Modern alternative to cron with better logging and dependency management.
# /etc/systemd/system/backup.service
[Unit]
Description=Daily Backup
[Service]
Type=oneshot
ExecStart=/home/sovon/scripts/backup.sh
User=sovon# /etc/systemd/system/backup.timer
[Unit]
Description=Run backup daily at 2 AM
[Timer]
OnCalendar=*-*-* 02:00:00
Persistent=true
[Install]
WantedBy=timers.target# Enable and start the timer
sudo systemctl daemon-reload
sudo systemctl enable backup.timer
sudo systemctl start backup.timer
# Check timer status
systemctl list-timers
systemctl status backup.timer
# Manually trigger the service
sudo systemctl start backup.service| Feature | Cron | Systemd Timer |
|---|---|---|
| Logging | Manual | journalctl |
| Dependencies | None | Full systemd deps |
| Missed runs | Lost | Persistent=true |
| Resource control | None | cgroups |
| Randomized delay | None | RandomizedDelaySec |
Anacron ensures daily/weekly/monthly jobs run even if the machine was off at the scheduled time.
cat /etc/anacrontab
# period delay job-identifier command
1 5 cron.daily run-parts /etc/cron.daily
7 10 cron.weekly run-parts /etc/cron.weekly
@monthly 15 cron.monthly run-parts /etc/cron.monthly- Cron: Create a cron job that logs the date to a file every 5 minutes
- Schedule: Set up a cron job to clean
/tmpfiles older than 7 days at midnight - at: Schedule a one-time command using
atfor 10 minutes from now - Timer: Create a systemd timer that runs a script every hour
- List: View all scheduled cron jobs and systemd timers
- Log: Set up a cron job with proper output redirection
- Verify: Check cron logs to confirm your job ran:
grep CRON /var/log/syslog
β Previous: SSH & Remote Access Β· π Home Β· Next: System Monitoring & Logs β