Introduction To Cron
In my previous entry Cron For Windows I discussed using cron as a better replacement for Windows Scheduled Task. Today we will look at how easy cron is to configure and look at a few examples that I have configured.
This tuturial will assume you have downloaded nnCron Lite and have it configured according to the online documentation. If you are on Mac or Linux you should already have cron available. nnCron’s format is the same as Unix so everything here should work anywhere you have cron available.
I installed nnCron to C:\Program Files\cron. Within that directory you should have a file called cron.tab. This is the main file used for starting and stopping your applications. While it’s not a GUI interface like Scheduled Tasks the configuration file is very easy to understand. Open cron.tab with Notepad or your favorite text editor.
The syntax is very simple…
Classic crontab format
Minutes Hours Days Months WeekDays Command
Some things to note: comments are preceeded by a “#” sign. Empty lines, leading spaces and tabs are ignored. nnCron also allows you to skip wrapping filenames and paths in quotations, and there is no need to call the command interpreter. Please see the nnCron documentation for more information.
For our first example we will run a batch file every morning to refresh my ColdFusion services to make sure everything is up and running when I get into the office. So assuming I have a batch file (cf8_restart.cmd) configured to stop and restart my ColdFusion services I can do the following:
restart web/cf services every morning
0 7 * * 1-5 D:_documents\scripts\batch\cf8_restart.cmd
The time format breaks down like:
0 = this is minutes - we want to run our script at the top of the hour 7 = equals hours - and we want to kick off our script at 7am (24hr format)
- = no value for days and months 1-5 = weekdays - we want this to only run M-F
For another example I run a backup utility to backup our JIRA instance:
run JIRA backup procedure every day at 12am and 12pm
0 0,12 * * * C:\Program Files\2BrightSparks\SyncBack\SyncBack.exe -m “JIRA Backup”
0 = this is minutes again - we want to run our script at the top of the hour 0,12 = equals hours - in this case I backup every 12 hours - at midnight and noon
- = no value for days, months and weekdays - this script runs everyday
These examples are very simple but the cron format can do very complicated schedules.
Here are a few examples taken from the nnCron documentation:
- 59 23 31 12 5 - One minute before the end of year if the last day of the year is Friday
- 45 17 7 6 * - Every year, on June 7th at 17:45
- 0 9 1-7 * 1 - First Monday of each month, at 9 a.m.
- 0 0 1 * * - At midnight, on the first day of each month
If you are using Windows Scheduled task and looking for more flexibility and reliability in your scheduled tasks I would encourage you to check out cron.