Jul 11, 2019

First of all let me introduce you to cron jobs, their syntax and how to create them. A cron job is a scheduled task performed by the system at a specified time / date.


The command to create / edit, list and remove a cron job is crontab. If you call it with the -u option, the tasks of the user whose crontab is called will be changed, not those of the logged in user. If this option is not met, crontab checks your crontab, e.g. If you are logged in as root and execute crontab without -u, then root’s crontab is listed / modified / removed. If you are logged in as exampleuser and start crontab without -u, then the crontab of exampleuser is , Listed / modified / removed.


To list the cron jobs of the user you are currently logged in as:

$ crontab -l

To list all the cron jobs of a specific user i.e. exampleuser:

$ crontab -u exampleuser -l

To create / modify Cron Jobs by the user you are currently logged in as:

$ crontab -e

To create / modify Cron Jobs by the user exampleuser:

$ crontab -u exampleuser -e 

To delete all cron jobs from the user you are currently logged in as:

$ crontab -r

To delete all cron jobs from user exampleuser:

$ crontab -u exampleuser -r

If you have written your cron jobs to a text file, you can use the file to create cron jobs. For example, let’s assume you created the text file /tmp/my_cron_jobs.txt …

$ nano /tmp/my_cron_jobs.txt 

with the following content:

20 00 * * * /path / to / script 

You can create a cron job from the file as follows:

$ crontab /tmp/my_cron_jobs.txt 

Please be aware that this will override all cron jobs created so far, if you have already created cron jobs, you should use crontab -e instead and create a new cron job manually.

To learn more about the crontab command just type the following command in your terminal

$ one crontab 

CRON JOB syntax

A cron job consists of these six attributes: ’minute’ ’hour’ ’day of month’ ’day of week’ ’command’ field allowed values:

minute 0-59

hour 0-23

day of month 1-31

month 1-12

day of week

If you set the day of the week, both day 0 and day 7 refer to Sunday (weekends are longer in the Linux world..hooray!)

A field with an asterisk (*) stands for all, i.e. all days of the week.

Lets check the following two examples:

* * * * * /usr/local/ispconfig/server/> / dev / null 2 ​​>> /var/log/ispconfig/cron.log 

That means: run /usr/local/ispconfig/server/> / dev / null 2 ​​>> /var/log/ispconfig/cron.log once per minute.

30 07 * * * /usr/local/ispconfig/server/> / dev / null 2 ​​>> /var/log/ispconfig/cron.log 

That means: run /usr/local/ispconfig/server/> / dev / null 2 ​​>> /var/log/ispconfig/cron.log once a day at 07: 30h .

The day on which the command is to be executed can be specified in 2 fields: Day of the month and Day of the week: If both fields are restricted (i.e no *), the command becomes valid if one of the two is valid at the moment Example, 15 2 1.15 * 5 would mean a command that would start at 2:15 am on the first and last month of every month, just like every Friday.


Now that we introduced cron jobs, let us make our first bot which will automatically backup a specific directory, compress it and save it in a different directory. This can be quite handy in various scenarious, especially if you have a website hosted on a VPS and you are in full control of the system.

Websites folders are usually stored in the following path /var/www/html/ where is the folder containing all your website directories and files (assuming it’s a static website with no database).

In order to create the cron job we need just type the following command:

$ crontab -e 

and add the following:

30 12 * * * tar -cvpzf / /var/www/  

This will create a backup of the whole website directory everyday at 12:30am. You can then arrange for the new backup to replace the existing one of create another cron job and rename the old ones if you wish to have an archive of the backups

Should anything happen to your live website, you will always have an up to date backup to restore from.


Last updated 28 Sep. 2020. Built with Emacs 27.1 (Org mode 9.3.7).