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:

    crontab -l
  • Edit the crontab file:

    crontab -e
  • Remove all crontab entries:

    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:

    * * * * * /path/to/script.sh
  • Run every hour:

    0 * * * * /path/to/script.sh
  • Run daily at midnight:

    0 0 * * * /path/to/script.sh
  • Run at 10:00, 12:00, and 14:00 every day:

    0 10,12,14 * * * /path/to/script.sh
  • Run every Monday and Friday at 3:30 PM:

    30 15 * * 1,5 /path/to/script.sh
  • Run every hour between 9 AM and 5 PM:

    0 9-17 * * * /path/to/script.sh
  • Run every day from the 1st to the 15th of the month at midnight:

    0 0 1-15 * * /path/to/script.sh
  • Run every 5 minutes:

    */5 * * * * /path/to/script.sh
  • Run every 2 hours:

    0 */2 * * * /path/to/script.sh
  • Run every other day:

    0 0 */2 * * /path/to/script.sh
  • Run every 5 minutes during working hours (9 AM to 5 PM):

    */5 9-17 * * * /path/to/script.sh
  • Run at 12:15 PM every Monday, Wednesday, and Friday:

    15 12 * * 1,3,5 /path/to/script.sh
  • Run on the 1st and 15th of every month at midnight:

    0 0 1,15 * * /path/to/script.sh

Special Strings

  • @reboot: Run once, at startup:

    @reboot /path/to/script.sh
  • @daily or @midnight: Run daily at midnight:

    @daily /path/to/script.sh
  • @hourly: Run every hour:

    @hourly /path/to/script.sh
  • @weekly: Run every Sunday at midnight:

    @weekly /path/to/script.sh
  • @monthly: Run on the 1st of the month at midnight:

    @monthly /path/to/script.sh
  • @yearly or @annually: Run on January 1st at midnight:

    @yearly /path/to/script.sh