A crontab file consists of optional variables at the top, followed by a list of scheduled tasks. The first five values of the task are the minute (m), hour (h), day-of-month (dom), day-of-week (dow), and the last value contains the command to be executed.
Here are some variables that you may choose to declare.
As mentioned, the first five fields determine when the cron job will be run. The last field is the command, and you may optional add a user before this parameter. Let's take a look at the scheduling parameters available.
There are also symbols that help you fine-tune a schedule.
/). Thus, the following command would run every three hours from 6am to midnight.
MAILTOemail@example.com SHELL=/bin/bash 0 6-21,0/3 * * * echo "Run every three hours throughout the day."
Cron also has available special keywords per your convenience.
Within the /etc directory, you can find subdirectories called cron.hourly, cron.daily, cron.weekly and cron.monthly. You may play a crontab in any of these directories to have it run hourly, daily, weekly or monthly.
Here is another example you can see setup by default on Ubuntu 14.01. You can see that each specific /etc directory is set to trigger each corresponding /etc/cron.* tab.
$ cat /etc/crontab # /etc/crontab: system-wide crontab # Unlike any other crontab you don't have to run the `crontab' # command to install the new version when you edit this file # and files in /etc/cron.d. These files also have username fields, # that none of the other crontabs do. SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin # m h dom mon dow user command 17 * * * * root cd / && run-parts --report /etc/cron.hourly 25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily ) 47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly ) 52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
Here are some examples. Try to guess the schedule per cron job before reading the output.
30 4 echo "It is now 4:30 am." 0 22 echo "It is now 10 pm." 30 15 25 12 echo "It is 3:30pm on Christmas Day." 30 3 * * * echo "Remind me that it's 3:30am every day." 0 * * * * echo "Ding. Dong. Ding. It is the start of a new hour." 0 6 1,15 * * echo "Set alarm for 6am on the 1st and 15th of every month." 0 6 * * 2,3,5 echo "Perform command at 6am on Tuesday, Wednesday and Thursdays." 59 23 * * 1-5 echo "Run a backup command before midnight on weekdays." 0 */2 * * * echo "Run something every two hours, on the dot." 0 20 * * 4 echo "8pm on a Thursday means take out the trash!" 0 20 * * Thu echo "8pm on a Thursday means take out the trash!" */15 9-17 * * 2-5 echo "Runs every 15 minutes from 9am-5pm on weekdays. GET TO WORK!" @yearly echo "Happy New Year!" @reboot echo "Let's get some work done today."
Linux for Beginners doesn't make any assumptions about your background or knowledge of Linux. You need no prior knowledge to benefit from this book. You will be guided step by step using a logical and systematic approach. As new concepts, commands, or jargon are encountered they are explained in plain language, making it easy for anyone to understand.$ Check price
Ever feel achy from sitting crunched up on your computer table? Try lying down with these optical glasses that allow you to work on your laptop while lying flat on your back. This is the perfect solution with those with limited mobility or those who wish to prevent neck cramps and back strains.$ Check price