Schedule jobs using Crontab

Data Geek
2 min readFeb 2, 2021

A Crontab is a program that enables UNIX users to execute scripts automatically at a specified time. Crontab stands for “cron table” because it uses the job scheduler cron to complete the tasks. Cron is the system process that will automatically perform tasks for you according to a set scheduler called crontab.

Available Options:

  • crontab -l : Display the current crontab
  • crontab -r : Remove the current crontab.
  • crontab -e : Edit the current crontab, using the editor specified in the VISUAL or EDITOR environment variables.
  • crontab -i : Same as -r, but gives the user a “Y/n” prompt before removing the crontab

Crontab Format:

* * * * * /bin/execute/this/script.sh 

These five stars represent different date parts in the following order:

  • Minute (from 0 to 59)
  • Hour (from 0 to 23)
  • Day of the month (from 1 to 31)
  • Month (from 1 to 12)
  • Day of the week (from 0 to 6) (0=Sunday)

You can specify multiple values using the below format.

  • The asterisk (*) operator specifies all possible values for a field. Example: Every hour or every day.
  • The comma (,) operator specifies a list of values. Example: “1, 3, 4, 7, 8”.
  • The dash (-) operator specifies a range of values. Example: “1–6”, which is equivalent to “1, 2, 3, 4, 5, 6”.
  • The slash (/) operator, can be used to skip a given number of values.
  • Example: “*/3” in the hour time field is equivalent to “0, 3, 6, 9, 12, 15, 18, 21”; “*” specifies ‘every hour’ but the “/3” means that only the first, fourth, seventh…and such values are given by “*” are used.

Steps to schedule Crontab job:

  • Crontab –e
  • Go to insert mode(press “I”)
  • Insert the script according to the requirement.

Example: 00 08 05 * * /bin/execute/this/script.sh

Examples:

  • Run the shell script /bin/execute/this/script.sh on January 2 at 6:15 A.M.
15 6 2 1 * /bin/execute/this/script.sh 
  • Run /bin/execute/this/script.sh every hour, on the hour, from 9 A.M. through 6 P.M., every day.
0 9–18 * * * /bin/execute/this/script.sh 
  • Run /bin/execute/this/script.sh every Monday, at 9 A.M. and 6 P.M.
0 9,18 * * Mon /bin/execute/this/script.sh 
  • Run /bin/execute/this/script.sh at 10:30 P.M., every weekday.
30 22 * * Mon,Tue,Wed,Thu,Fri /bin/execute/this/script.sh

--

--

Data Geek

I write about Python, Unix, Data Engineering, and Automation Testing.