Problem:
Imagine a scenario where you need to run a program on a specific day, at a specific time. Lets say you need to run a program every Sunday at 3 AM. And you also need that the program sends you an email with its output and log to a file.
Solution:
Let us say that the program which I want to execute is named program. The email address to which the output is supposed to be mailed is assumed as [email protected] The log is saved to a file named mylog_file.log. This scheduling can be done by making a crontab file. The content of my crontab file (Let me name it as my_crontab_file) would be the following:
MAILTO=”[email protected]
”
0 03 * * sun /path/to/program > /path/to/mylog_file.log 2>&1
Field descriptions (from left to right):
0 – Minute (0-59)
03 – Hour (0-23)
* – Day of Month (1-31, * means all days of the month)
* – Month of Year (1-12, * means all months of the year)
sun – Day of the week (1-7 or sun-mon-tue-wed-thurs-fri-sat, * means all days of the week)
The program is run at that time and its output (along with error using 2>&1) is sent to a log file. This file is then needs to be declared as crontab file as following:
crontab my_crontab_file