Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

What Is Cron in Linux and How Do You Use It?

· 7 min read · Home Tech

In this article
  1. Background: Where Cron Comes From
  2. How Cron Works
  3. Writing Your First Cron Job
  4. Common Time Patterns and Shortcuts
  5. Redirecting Output and Logging
  6. Common Problems and How to Fix Them
  7. Security and Permissions
  8. Alternatives to Cron
  9. Frequently Asked Questions

Cron is a standard Linux job scheduler that runs commands or scripts automatically at specified times or intervals. You use it by editing a crontab file with the crontab -e command, writing a line with five time fields followed by the command, then saving the file. The cron daemon checks these files every minute and runs whatever is due.

Background: Where Cron Comes From

The name "cron" comes from the Greek word chronos, meaning time. It was introduced in Unix Version 7 in 1979, written by Ken Thompson. Since then it has been reimplemented many times: Vixie cron (the classic Linux version), cronie (the default on most modern distributions), and systemd timers (a newer alternative). The word itself is standard technical terminology, not slang or informal. You would say "I set up a cron job to back up the database nightly" or "Check the crontab to see what's scheduled."

How Cron Works

Cron is a daemon, a background process that starts at boot and sleeps most of the time. Every minute it wakes up, reads the crontab files, and compares the current time against the schedule patterns. If a line matches, it runs the command as the user who owns that crontab. The output of the command is emailed to the user by default, though most people redirect it to a log file instead.

Each user has their own crontab, and there is also a system crontab in /etc/crontab plus drop-in directories like /etc/cron.d. The system crontab includes an extra field for the username. Regular users only manage their own jobs with the crontab command.

Writing Your First Cron Job

The basic crontab syntax has five time fields followed by the command:

minute hour day-of-month month day-of-week command

For example, this line runs /home/user/backup.sh every day at 2:30 AM:

30 2 * * * /home/user/backup.sh

The asterisks mean "every" unit. So * * * * * runs every minute. To run every hour at minute 0, use 0 * * * *. To run every Monday at 9 AM, use 0 9 * * 1 (Sunday is 0 or 7, Monday is 1).

To edit your crontab, type:

crontab -e

This opens the file in your default text editor. After saving, cron picks up the changes automatically. To list your jobs, use crontab -l. To remove all your jobs, use crontab -r (this deletes everything without confirmation, so be careful).

Common Time Patterns and Shortcuts

Instead of the five fields, cron supports a few shorthand strings:

  • @reboot – run once when the computer starts
  • @daily or @midnight – run once a day at 00:00
  • @hourly – run at minute 0 of every hour
  • @weekly – run at 00:00 on Sunday
  • @monthly – run at 00:00 on the first of the month
  • @yearly or @annually – run at 00:00 on January 1

You can also use ranges like 9-17 for hours, lists like 1,15 for days, and step values like */15 in the minute field to run every 15 minutes. For example, */15 * * * * runs every quarter hour.

Redirecting Output and Logging

By default, cron sends any output from your command to your email. On most systems that email goes nowhere unless you set up a mail agent. To avoid losing output, redirect it yourself. Append this to your command:

30 2 * * * /home/user/backup.sh >> /tmp/backup.log 2>&1

This appends standard output and standard error to /tmp/backup.log. The 2>&1 sends error messages to the same place. Use > (single bracket) if you want to overwrite the file each run instead of appending.

If your script needs environment variables like PATH, set them at the top of the crontab. For example:

PATH=/usr/local/bin:/usr/bin:/bin

This is a common source of errors: cron runs with a minimal environment, so commands that work in your terminal may fail because the full PATH is missing.

Common Problems and How to Fix Them

The most frequent cron issue is a job that does not run. Check these in order:

  1. Confirm the cron service is running: systemctl status cron (or crond on some systems). If it is not active, start it with sudo systemctl start cron.
  2. Verify the crontab syntax with crontab -l and make sure the command path is absolute (e.g., /usr/bin/backup instead of just backup).
  3. Check the log file. On Debian/Ubuntu it is /var/log/syslog; on RHEL/CentOS it is /var/log/cron. Search for your command name or the word CRON.
  4. Make sure the script itself is executable (chmod +x script.sh) and has the correct shebang line like #!/bin/bash.
  5. Test the command manually in a terminal. If it fails there, fix it before blaming cron.

Another common mistake is misunderstanding the day-of-week and day-of-month fields. If you set both to a specific value, cron runs when either matches, not both. For example, 0 0 1 * 1 runs on the first of the month AND on every Monday, not only when the first falls on a Monday. If you need that logic, use a script with a condition.

Security and Permissions

By default, any user can create crontabs. System administrators can restrict this with /etc/cron.allow and /etc/cron.deny. If cron.allow exists, only users listed there may use cron. If only cron.deny exists, everyone except those listed may use it.

Be careful with commands that run as root. A typo in a root crontab can delete files or break the system. Always test scripts as a normal user first, and use absolute paths. If you need to run a job as another user, edit the system crontab (sudo crontab -e for root, or the /etc/crontab file with the username field).

Alternatives to Cron

On modern Linux systems with systemd, you can use systemd timers as a more flexible alternative. Timers are configured in unit files and can run jobs with more precise control, such as running a job a specific number of times after boot or with random delays. However, they are more verbose to set up than a single crontab line.

For one-off tasks in the future, use at (e.g., at 5pm then type your command). For recurring tasks that need to run only while the system is on, cron is still the simplest and most widely supported option.

Frequently Asked Questions

How do I see what cron jobs are currently scheduled?

Type crontab -l to list your own jobs. To see system-wide jobs, look at /etc/crontab and the files in /etc/cron.d, or check /etc/cron.hourly, /etc/cron.daily, etc. Note that the crontab -l command only shows the current user's jobs, not root's or other users'.

Why did my cron job not run?

Start with the checks in the Common Problems section: verify the service is running, confirm the syntax, check the logs, and test the command manually. The most common causes are a missing PATH, a non-executable script, or a typo in the time fields. Also remember that cron uses the system's timezone, not your shell's, so a timezone mismatch can make a job appear to run at the wrong time.

Can I run a cron job every 30 seconds?

No, cron's minimum resolution is one minute. If you need sub-minute intervals, write a loop inside your script that sleeps for 30 seconds, or use a systemd timer with OnUnitActiveSec=30s. Another common workaround is to have the cron job start a background process that loops, but that can leave orphan processes if not handled carefully.

How do I edit the root crontab?

Use sudo crontab -e. This edits the crontab for the root user, which runs commands with full system privileges. Be extremely careful with this, as a mistake can affect the whole system. Always double-check the command and the time fields before saving.

Does cron run jobs if the computer is off at the scheduled time?

No. Cron only runs jobs when the system is actually on. If the computer is powered off, the job is skipped, not run later. If you need to run missed jobs after boot, consider using @reboot or a systemd timer with Persistent=true, which runs missed timers after the system starts.

Is cron safe to use for important tasks?

Yes, cron is reliable and has been used for decades. However, you should always redirect output to a log file, test the command manually, and monitor the logs periodically. For critical jobs, consider adding a second mechanism (like a monitoring script) to alert you if the job fails.