Difference between revisions of "Setting Up a Cron Job"

From rbachwiki
Jump to navigation Jump to search
 
(15 intermediate revisions by the same user not shown)
Line 4: Line 4:
* Click on the (+) icon to add an item to the list.
* Click on the (+) icon to add an item to the list.
* Press command+shift+G, type /usr/sbin/cron and press enter:
* Press command+shift+G, type /usr/sbin/cron and press enter:
* Select the cron exexcutable and click Open
* Select the cron executable and click Open


==Vim Editor Commands==
shift + :
* :i // insert text
* esc key // get out of insert mode
* :wq Write file to disk and quit the editor


== Setting up a Cron Job ==
== Setting up a Cron Job ==


  crontab -e  ** use the -e switch, do not edit the file directly  
  crontab -e  # use the -e switch, do not edit the file directly  
 
  crontab -l # shows scheduled jobs
0 7 */15 * * /var/www/scripts/htmlbackup.sh 2>> /var/www/cronhtml.txt
  crontab -r  # remove the current crontab file
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 ==
== Crontab sections ==
Line 26: Line 26:
|-
|-
|*||*||* ||*||*
|*||*||* ||*||*
|-
|0-59||0-23||1-31 ||1-12 or jan, feb,mar,..||0-6 or sun, mon, tue
|}
{| class="wikitable" style="margin:auto"
|Every Minute||* * * * *||Ever 5 Minute||*/5 * * * *||Ever 30 Minutes||*/30 * * * *
|-
|Every Hour||0 * * * *||Every Hour and 30 min||30 * * * *||Ever 2 hours||0 */2 * * *


|}
|}
Line 31: Line 40:
   
   
* minute of the hour the command will run on, and is between '0' and '59'
* minute of the hour the command will run on, and is between '0' and '59'
*hour the command will run on, and is specified in the 24 hour clock, values must be between 0 and 23 (0 is midnight)
* hour the command will run on, and is specified in the 24 hour clock, values must be between 0 and 23 (0 is midnight)
*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.
* 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.
*month a specified command will run on, it may be specified numerically (0-12), or as the name of the month (e.g. May)
* month a specified command will run on, it may be specified numerically (0-12), or as the name of the month (e.g. May)
* 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).
* 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).
* user who runs the command.
* user who runs the command.
*cmdThis is the command that you want run. This field may contain multiple words or spaces.
* 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
* 12 10-16/2 * * /scripts/backup.sh


must be on a line by them self.  
<p class="subhead">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.</p>
<p class="subhead">Comments are allowed in crontabs, but they must be preceded with a '#', and must be on a line by them self. </p>


----
----


==[[#Setting up a Cron Job|Back To Top]]-[[Main_Page| Home]] - [[Ubuntu_Tips|Category]]==
==[[#Setting up a Cron Job|Back To Top]]-[[Main_Page| Home]] - [[Ubuntu_Tips|Category]]==

Latest revision as of 15:03, 17 June 2024

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 executable and click Open

Vim Editor Commands

shift + :
  • :i // insert text
  • esc key // get out of insert mode
  • :wq Write file to disk and quit the editor

Setting up a Cron Job

crontab -e  # use the -e switch, do not edit the file directly 
crontab -l # shows scheduled jobs
crontab -r  # remove the current crontab file

Crontab sections

Parts of cron tab
Minute Hour Day (month) Month Day (week)
* * * * *
0-59 0-23 1-31 1-12 or jan, feb,mar,.. 0-6 or sun, mon, tue
Every Minute * * * * * Ever 5 Minute */5 * * * * Ever 30 Minutes */30 * * * *
Every Hour 0 * * * * Every Hour and 30 min 30 * * * * Ever 2 hours 0 */2 * * *


  • minute of the hour the command will run on, and is between '0' and '59'
  • hour the command will run on, and is specified in the 24 hour clock, values must be between 0 and 23 (0 is midnight)
  • 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.
  • month a specified command will run on, it may be specified numerically (0-12), or as the name of the month (e.g. May)
  • 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).
  • user who runs the command.
  • command that you want run. This field may contain multiple words or spaces.
* 12 10-16/2 * * /scripts/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.


Back To Top- Home - Category