crontab
command in Linux but as you will soon see, the command is very simple and hence should be easy to master. There are some nuances that I learnt during the course of the work and I will discuss it at the end.The crontab command begins with setting the time, date at which a particular job needs to be run. These parameters are set using a crontab file, which can be edited by calling the command
crontab -l
. In the editor, type the following command5 * * * * command-to-execute > logfile.log
The number 5 represents the minute at which the command has to be executed. The subsequent values represents the hour, day, month and day of the week (Sunday = 0) respectively. The value * represents all the possible combination. The > pipes the output of the command to a logfile. Once the crontab file has been set, it can be verified using
crontab -l
. The crontab file can also be written to a text file and loaded using crontab cronfile.txt
. The content of the cronfile.txt is same as above.If you wish that you receive a mail every time the cron runs, add the following line first
MAILTO: johndoe@email.com (Of course, replace it with the correct email address)
So far, everything is easy and good but be careful with the following,
1.Ensure that there is an empty line after the last line of crontab file.
2.For some reason, crontab cannot use the PATH stored by Linux. So ensure that all commands in the crontab file and all the commands and scripts called by crontab are referred with full path.
So instead of calling
* 2 * * * python /path/to/file/pythonfile.py
call
* 2 * * * /usr/bin/python /path/to/file/pythonfile.py
Also make sure that all the path in the python file are also referred using full path.