CronAutomationTasks5 min read

Schedule tasks with Cron

Schedule automated tasks with Cron to run scripts and commands at defined intervals.


Cron is the standard task scheduler on Linux. Use it to automate backups, maintenance scripts, log rotation and more.

Crontab syntax

terminal
┌───────────── 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

Edit your crontab

bash
crontab -e

Choose your preferred editor (nano is easiest).

Common examples

bash
# Every day at 3:00 AM — database backup
0 3 * * * /home/deploy/scripts/backup-db.sh

# Every hour — clear temp files
0 * * * * rm -rf /tmp/cache/*

# Every Monday at 6:00 AM — system update
0 6 * * 1 sudo apt update && sudo apt upgrade -y

# Every 5 minutes — health check
*/5 * * * * curl -s https://mydomain.com/health > /dev/null

# First day of each month — log rotation
0 0 1 * * /usr/sbin/logrotate /etc/logrotate.conf

View current crontab

bash
crontab -l

Redirect output to a log

bash
0 3 * * * /home/deploy/scripts/backup.sh >> /var/log/backup.log 2>&1

System-wide cron jobs

For root-level tasks, edit:

bash
sudo nano /etc/crontab

Or place scripts in:

  • /etc/cron.daily/
  • /etc/cron.weekly/
  • /etc/cron.monthly/

Special strings

bash
@reboot   command  # Run once at startup
@daily    command  # Run once a day (midnight)
@weekly   command  # Run once a week
@monthly  command  # Run once a month
@hourly   command  # Run once an hour

Troubleshooting

  • Check cron logs: grep CRON /var/log/syslog
  • Ensure scripts are executable: chmod +x script.sh
  • Use full paths in cron (cron has a minimal PATH)
  • Test commands manually before adding to crontab

Was this guide helpful?