crontab
v.s cronjob
Crontab stands for “cron table.” It is a Unix-based utility that provides a configuration file used by the cron daemon (crond
), the background process that runs scheduled, automated tasks. The crontab file stores a list of cron jobs—each line represents a separate cron job.
A cron job is an individual task (e.g., running scripts, executing commands, etc.) specified/defined in the crontab file.
Crontab Commands
-
View current crontab entries:
Terminal window crontab -l -
Edit the crontab file:
Terminal window crontab -e -
Remove all crontab entries:
Terminal window crontab -r
Cronjob Format
* * * * * command_to_execute- - - - -| | | | || | | | +----- Day of the week (0–7, where 0 or 7 is Sunday, 1 is Monday, etc.)| | | +------- Month (1-12)| | +--------- Day of the month (1-31)| +----------- Hour (0-23)+------------- Minute (0-59)
Special Characters
*
: Match all possible values,
: Separate multiple values-
: Specify a range of values/
: Specify step values
Examples
-
Run a script every minute:
Terminal window * * * * * /path/to/script.sh -
Run every hour:
Terminal window 0 * * * * /path/to/script.sh -
Run daily at midnight:
Terminal window 0 0 * * * /path/to/script.sh -
Run at 10:00, 12:00, and 14:00 every day:
Terminal window 0 10,12,14 * * * /path/to/script.sh -
Run every Monday and Friday at 3:30 PM:
Terminal window 30 15 * * 1,5 /path/to/script.sh -
Run every hour between 9 AM and 5 PM:
Terminal window 0 9-17 * * * /path/to/script.sh -
Run every day from the 1st to the 15th of the month at midnight:
Terminal window 0 0 1-15 * * /path/to/script.sh -
Run every 5 minutes:
Terminal window */5 * * * * /path/to/script.sh -
Run every 2 hours:
Terminal window 0 */2 * * * /path/to/script.sh -
Run every other day:
Terminal window 0 0 */2 * * /path/to/script.sh -
Run every 5 minutes during working hours (9 AM to 5 PM):
Terminal window */5 9-17 * * * /path/to/script.sh -
Run at 12:15 PM every Monday, Wednesday, and Friday:
Terminal window 15 12 * * 1,3,5 /path/to/script.sh -
Run on the 1st and 15th of every month at midnight:
Terminal window 0 0 1,15 * * /path/to/script.sh
Special Strings
-
@reboot: Run once, at startup:
Terminal window @reboot /path/to/script.sh -
@daily or @midnight: Run daily at midnight:
Terminal window @daily /path/to/script.sh -
@hourly: Run every hour:
Terminal window @hourly /path/to/script.sh -
@weekly: Run every Sunday at midnight:
Terminal window @weekly /path/to/script.sh -
@monthly: Run on the 1st of the month at midnight:
Terminal window @monthly /path/to/script.sh -
@yearly or @annually: Run on January 1st at midnight:
Terminal window @yearly /path/to/script.sh