About technologies for your digital home

For beginners and tinkerers

Crontab format

November 17th, 2016

Once you learn basic Linux commands and at least a bit Linux shell (e.g. Bash) scripting, crontab (scheduler) in Linux becomes another major step forward, as it allows you to automate your shell scripts. This post is about crontab and its format.

Before using crontab

Just make sure that your bash scripts are executable. You can simple do this by executing this command to make a file executable:

chmod +x filename.sh

You can read more on file permissions in my previous post on basic Linux commands.

Crontab commands

Basically, there are two main commands – list and edit.

You can get the crontab jobs (scripts) by entering the command crontab -l.

Keep also in mind, that every user on linux system can have separated crontab jobs. So, by entering

crontab -l

you are revising what is setup to run under current user.

Another command is for editing:

crontab -e

When you run for the first time, the system will ask you which editor to you want to use for editing crontab jobs:

Select an editor. To change later, run 'select-editor'.
1. /bin/ed
2. /bin/nano <---- easiest
3. /usr/bin/vim.basic
4. /usr/bin/vim.tiny

Choose 1-4 [2]:

Crontab format

The format of a crontab is 6 columns, space (or tab) separated and it follows:

m h dom mon dow command

m – minutes with the range [0-59]

h – hours with  the range [0-23]

dom – day of the month with the range [1-31]

dow – day of week with range [1-7, 1st day is Monday]

Any of these fields can be an asterisk (*). This would mean the script would be repeated each minute, each hour, … All this gives a lot of the flexibility to run the jobs on very specific times.

Few simple examples

*/15 * * * * /path_to_script/script.sh would run every 15 minutes

or you can easily schedule to run every 5 minutes

*/5 * * * * /path_to_script/script.sh
0 0 * * * * /path_to_script/script.sh would execute your scripts 
daily at midnight
0 9-18 * * * /path_to_script/script.sh would run every hour, 
from 9 A.M. through 6 P.M., every day
0 1 * * 5 /path_to_script/script.sh  would execute every Friday 1AM
0 1 * * 1-5 /path_to_script/script.sh would execute on workdays 1AM

Any field may contain a list of values separated by commas, (e.g. 1,3,7) or a range of values (two integers separated by a hyphen, e.g. 1-5).

Reasons why crontab does not work

Here are the potential sources for the crontab not run properly:

  • wrong crontab notations;
  • permissions problems;
  • environment variable(s);
  • different environments (shells).