- type
crontab -e
to open the crontab file, this is where you add a task for cron to perform. - have to specify the path to all the functions in the terminal, cronjob by default won’t recognize any function that you type, example if you create a cron task that will run a php script like: php phpscript.php it will not recognize the function php unless you specify the path.
- to specify all the function paths on the machine to for cron you have to add this line in the top of the crontab -e file.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
- first 5 elements in a cron task line is to define the time to run the task.
- format:
* * * * * sh script.sh
- The first five fields are: [minute 0-59] [hour 0-23] [day of the month 1-31] [month of the year 1-12] [day of the week 0-6 with 0 = Sunday, 1= Monday] http://techsk.blogspot.ca/2008/06/how-to-run-cronjob-on-last-friday-of.html
-
* * * * * * sh /home/sonal/cronjob/cron_email.sh
The shell script just contains a sendmail command that will send an email with a text files as the body.#!/usr/bin/env bash
sendmail sonal.ranjit3@gmail.com < "/home/sonal/cronjob/test.txt"
-
The five stars for the time field will run this shell script every minute for every hour of the month for every month of the year and everyday of the week.
00 01 * * 5 [ $(date +"\%m") -ne $(date -d 7days +"\%m") ] && sh /var/lib/postgresql/cronjob/cron_email.sh
- The first crontab is set to send a reminder email every last friday of the month listing all the files that are present on the folder system and to remind to download the york region data.
-
When creating crontabs have to be mindful about full functionalities as shell, it is not the same. To escape double quotes on the crontab such as the condition above $(date +”%m”) the “\” is necessary to escape the % sign otherwise the cron will be interpreted as a new line after the % which will render the crontab useless.
-
http://serverfault.com/questions/274475/escaping-double-quotes-and-percent-signs-in-cron