Select your language

Scripts like backups, notifications and others need to run in intervals or fix dates. Crontab provides a possibility to do that.

Create a Cronjob and understand the sytax

Crontab is the table which holds all the individual Cronjobs. You can easily open the Crontab in the Terminal with

sudo crontab -e

If it is your first time opening the Crontab will you be asked to choose one preferred editor.

New Cronjobs are added at the bottom in a strict syntax. It is explained a bit in the Crontab itself but here is a more elaborate explanation.

You can work with minutes, hours, days, months and weekdays. Acceptable values are either integers in the specified ranges or a star. The star has the meaning of "all".

 

If you want to run a script "all 10 minutes" do the following:

*/10 * * * * python3 /path to my script

All unused fields (for hour etc.) remain empty and are represented by a star.

A Cronjob running every Monday (1st day of the week) at quarter past 10 am would look like that:

15 10 * * 1 python3 /path to my script

 There is also the possibility to execute a Cronjob at certain events like reboot and others

@reboot pyhton3 /path to my script

Other keywords are

Keyword Function  
@daily every day at 00:00 hours  
@hourly every full hour  

Miscellaneous

It is important to note that a Cronjob does not open a terminal where you can see messages or exceptions which are eventually created by your script unless you route that output to a file or an e-mail.

Output to file

Just add

>> path to log file

and you will find the output there. So, if you have a script which sends a mail every christmas day and you want to check the success message your Cronjob would look like that

1 1 24 12 * python3 /var/send_xmas_mail.py >> /var/xmas_mail.log

Output to e-mail

The MAILTO variable needs to be set and your are good to go.

MAILTO This email address is being protected from spambots. You need JavaScript enabled to view it.
1 1 24 12 * python3 /var/send_xmas_mail.py

 Sources / Links

The Wiki Ubuntuuser provides detailed information on the Crontab.