Linux Crontab

Crontab could help you run a task at fixed intervals. It's so easy to use crontab because it has only three main commands: crontab -l, crontab -r and crontab -e.

crontab -l will list all your tasks. crontab -r will delete all your tasks. Please be especially careful when using it. The most important command is crontab -e which allows you to edit your tasks.

Select an editor

What crontab -e actually does is to open your crontab file which contains all your scheduled tasks. So for the first time you run crontab -e it will ask you to select an editor:

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

Choose 1-4 [1]: 3

Just select an editor you like. If you select a wrong editor like I do don't worry. Just run select-editor to select again. You can also delete the file ~/.selected_editor then for the next time you run crontab -e you will be asked to select an editor again.

Set time

Each line in the crontab file is a scheduled task. The comment content below is intuitive enough to explain how to set time.

# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of week (0 - 6) (Sunday to Saturday;
# │ │ │ │ │                                       7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * *  command to execute

Leave it with * just means every time the symbol represents. For example * * * * * means every minute of every hour of every day of month of every month of every day of week.

Use , to indicate multiple time of the unit. For example 2,3 * * * * means the second and third minutes of every hour of every...

One import thing to guarantee crontab works is that using absolute path.