CRON Command Cheatsheet
CRON is a time-based job scheduler in Unix-like operating systems. It allows you to automate tasks by running commands or scripts at specific intervals - such as horly, daily, weekly, or even at custom times.
Key Concepts
Cron Daemon (crond)
The background service that executes scheduled tasks. Runs continuously and checks for tasks to run at their specified times.
Crontab (Cron Table)
A configuration file that defines the schedule and commands for cron jobs. Each user can have their own crontab file.
Cron Syntax
A cron job in system-wide crontab (/etc/crontab) and files in /etc/cron.d/* is defined in the following format:
* * * * * user command_to_executehere the 'user' is required; cron runs command as that user.
The five asterisks represent time and date fields:- Minute (0–59)
- Hour (0–23)
- Day of the month (1–31)
- Month (1–12)
- Day of the week (0–7, where both 0 and 7 represent Sunday)
For per-user crontab (from crontab -e) format is:
* * * * * command_to_executeExamples of Cron Jobs
Every minute:
* * * * * commandEvery 5 minutes:
*/5 * * * * commandDaily at 3 AM (explicit):
0 3 * * * commandEvery Monday at 5 PM
0 17 * * 1 commandFirst day of the month
0 0 1 * * commandCommon macros (alternative shorthand):
@hourly command@daily command@weekly command@monthly command@yearly commandRuns the 'command' once when the machine (or cron service) starts:
@reboot commandCrontab
Cron jobs are defined in user's contab file. They can be listed with
crontab -lOpening the crontab file for edit (add lines in the format above to add new jobs, then save and exit):
crontab -eWorth to redirect output to a log file for debugging (otherwise email is attempted if mail subsystem is set up):
* * * * * /path/to/script.sh >> /path/to/logfile.log 2>&1 Remove all Cron Jobs:
crontab -rSetting up environment variables for Cron jobs
Variables set at the top of the crontab apply globally to every entry in that file. In user crontab (crontab -e) this means all jobs inherit them; in /etc/crontab or /etc/cron.d/*, they apply the same way for that file.
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
SHELL=/bin/bash
MAILTO="" To override for a single job, include the env assignment on that line before the command or use a wrapper script with its own environment setup.
0 4 * * * /usr/bin/env VAR=value /usr/local/bin/mytask.sh >> /var/log/mytask.log 2>&1