Cron is a time‑based job scheduler on Linux. It runs commands or scripts at specified times – daily, weekly, or custom intervals.

This guide walks you through setting up cron jobs on Ubuntu.

What Can Cron Do?

Cron can automate almost anything: backup databases every night, renew SSL certificates weekly, clean temporary files hourly, send email reports daily, pull data from APIs every 15 minutes.

If you find yourself running the same command manually, cron can automate it.

Check If Cron Is Running

Most Ubuntu installations include cron by default. Check its status:

sudo systemctl status cron

If it's not running, start and enable it:

sudo systemctl start cron
sudo systemctl enable cron

Crontab Syntax Basics

Crontab is the file where cron jobs are defined. Each line has five time fields followed by the command.

The five time fields represent:

* * * * * command_to_run
│ │ │ │ │
│ │ │ │ └─── Day of week (0-7, 0 and 7 = Sunday)
│ │ │ └───── Month (1-12)
│ │ └─────── Day of month (1-31)
│ └───────── Hour (0-23)
└─────────── Minute (0-59)

Common examples:

  • 0 2 * * * – Run at 2:00 AM every day
  • */15 * * * * – Run every 15 minutes
  • 0 9 * * 1 – Run at 9:00 AM every Monday
  • 0 0 1 * * – Run at midnight on the first day of every month

Edit Your Crontab

To edit your user's crontab:

crontab -e

If it's your first time, select an editor (nano is beginner‑friendly).

Each line is a separate cron job. Add your command and save the file.

To edit the root user's crontab (for system‑wide tasks):

sudo crontab -e

Examples of Real Cron Jobs

Daily database backup at 2 AM:

0 2 * * * /usr/bin/mysqldump --all-databases > /backups/db_$(date +\%Y\%m\%d).sql

Weekly cleanup of old logs every Sunday at 3 AM:

0 3 * * 0 find /var/log -name "*.log" -mtime +30 -delete

Hourly sync of website files to backup server:

0 * * * * rsync -avz /var/www/ user@backupserver.com:/backups/

Reboot daily at 4:30 AM (not recommended):

30 4 * * * /sbin/reboot

Run a PHP script every 5 minutes:

*/5 * * * * /usr/bin/php /var/www/cron/refresh_data.php

Logging and Debugging

Cron jobs don't show output in the terminal. To log output, redirect to a file:

0 2 * * * /usr/bin/backup.sh >> /var/log/backup.log 2>&1

To receive errors via email, set the MAILTO variable at the top of your crontab:

MAILTO=admin@example.com

Check if cron ran a job by viewing system logs:

sudo grep CRON /var/log/syslog

Special Syntax Shortcuts

Cron supports special strings as shortcuts:

  • @reboot – Run once after reboot
  • @daily – Same as 0 0 * * *
  • @hourly – Same as 0 * * * *
  • @weekly – Same as 0 0 * * 0
  • @monthly – Same as 0 0 1 * *
  • @yearly – Same as 0 0 1 1 *

Example of @reboot:

@reboot /usr/bin/python3 /home/user/start_bot.py

View Existing Cron Jobs

List your current cron jobs:

crontab -l

Remove all cron jobs (careful):

crontab -r

Troubleshooting Common Issues

Cron job runs with wrong environment variables. Cron has a minimal environment. Use absolute paths to commands and files. Check with which command.

Script runs manually but not from cron. Add execute permission: chmod +x script.sh. Also ensure the script's shebang line is correct (e.g., #!/bin/bash).

Time zone issues. Cron uses the system time zone. Check with timedatectl. Change with sudo timedatectl set-timezone Your/Zone.

Testing a Cron Job Safely

Before relying on a cron job, test the command manually. Run the exact command from your terminal. If it works, add it to crontab.

Then set the cron job to run 2 minutes from now for testing:

2 * * * * /path/to/command

Check the output and logs. Once confirmed, change to the desired schedule.

Next Steps

Now that you can schedule tasks with cron, automate your routine server maintenance, backups, and updates.

Need a VPS to practice on? Check our recommended VPS providers.