Setting Up a Cron Job
Setting Cron to run on Mac Os
- Cron needs full disk access in order to run on mac
- Go to System Settings > Privacy & Security > Full Disk Access:
- Click on the (+) icon to add an item to the list.
- Press command+shift+G, type /usr/sbin/cron and press enter:
- Select the cron exexcutable and click Open
Setting up a Cron Job
crontab -e ** use the -e switch, do not edit the file directly
- * * * * /backup/script.sh
0 7 */15 * * /var/www/scripts/htmlbackup.sh 2>> /var/www/cronhtml.txt 0 4 */15 * * /var/www/scripts/sitesavailablebackup.sh 2>> /var/www/cronsites.txt
crontab -l - shows scheduled jobs
crontab -r remove the current crontab file
Crontab sections
Minute | Hour | Day (month) | Month | Day (week) |
---|---|---|---|---|
* | * | * | * | * |
- minute This controls what minute of the hour the command will run on, and is between '0' and '59'
hourThis controls what hour the command will run on, and is specified in
the 24 hour clock, values must be between 0 and 23 (0 is midnight)
domThis is the Day of Month, that you want the command run on, e.g. to
run a command on the 19th of each month, the dom would be 19.
monthThis is the month a specified command will run on, it may be specified
numerically (0-12), or as the name of the month (e.g. May)
dowThis is the Day of Week that you want a command to be run on, it can
also be numeric (0-7) or as the name of the day (e.g. sun).
userThis is the user who runs the command.
cmdThis is the command that you want run. This field may contain
multiple words or spaces.
Fields
.---------------- minute (0 - 59)
| .------------- hour (0 - 23)
| | .---------- day of month (1 - 31)
| | | .------- month (1 - 12) OR jan,feb,mar,apr ...
| | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
| | | | |
* * * * *
If you don't wish to specify a value for a field, just place a * in the
field.
e.g.
01 * * * * root echo "This command is run at one min past every hour"
17 8 * * * root echo "This command is run daily at 8:17 am"
17 20 * * * root echo "This command is run daily at 8:17 pm"
00 4 * * 0 root echo "This command is run at 4 am every Sunday"
- 4 * * Sun root echo "So is this"
42 4 1 * * root echo "This command is run 4:42 am every 1st of the month"
01 * 19 07 * root echo "This command is run hourly on the 19th of July"
Notes:
Under dow 0 and 7 are both Sunday.
If both the dom and dow are specified, the command will be executed when
either of the events happen.
e.g.
- 12 16 * Mon root cmd
Will run cmd at midday every Monday and every 16th, and will produce the
same result as both of these entries put together would:
- 12 16 * * root cmd
- 12 * * Mon root cmd
Vixie Cron also accepts lists in the fields. Lists can be in the form, 1,2,3
(meaning 1 and 2 and 3) or 1-3 (also meaning 1 and 2 and 3).
e.g.
59 11 * * 1,2,3,4,5 root backup.sh
Will run backup.sh at 11:59 Monday, Tuesday, Wednesday, Thursday and Friday,
as will:
59 11 * * 1-5 root backup.sh
Cron also supports 'step' values.
A value of */2 in the dom field would mean the command runs every two days
and likewise, */5 in the hours field would mean the command runs every
5 hours.
e.g.
- 12 10-16/2 * * root backup.sh
is the same as:
- 12 10,12,14,16 * * root backup.sh
- /15 9-17 * * * root connection.test
Will run connection.test every 15 mins between the hours or 9am and 5pm
Lists can also be combined with each other, or with steps:
- 12 1-15,17,20-25 * * root cmd
Will run cmd every midday between the 1st and the 15th as well as the 20th
and 25th (inclusive) and also on the 17th of every month.
- 12 10-16/2 * * root backup.sh
is the same as:
- 12 10,12,14,16 * * root backup.sh
When using the names of weekdays or months, it isn't case sensitive, but only
the first three letters should be used, e.g. Mon, sun or Mar, jul.
Comments are allowed in crontabs, but they must be preceded with a '#', and
must be on a line by them self.