Issue:
How can I schedule some tasks (like mirroring one disk to another) to occur automatically on a regular basis?
Solution:
The standard Unix command cron will manage the execution of tasks on a specified schedule. The first step is to create a command-shell script that executes the desired task. For example, I use a bash script containing an rsync command that mirrors the contents of my main hard drive to a secondary hard drive. Store this shell script in your home directory (the home directory on a Mac is at
/Users/{login}/; for example, I want this script to be executed daily, so I created a hidden sub-directory in my home directory called ~/.cron.daily/ and put the script file in it.
Tasks for cron to execute are listed in a crontab file. System-wide (i.e., root level) cron tasks are controlled by /etc/crontab, but for user-level tasks, a crontab file (or, in my case, a hidden .crontab file) can be created in the home directory. The format of the crontab file is like this:
# min hr mday month wday command 15 03 * * * sh /Users/{login}/.cron.daily/backup.sh 15 03 1 * * sh /Users/{login}/sample1.sh 15 03 * * 6 sh /Users/{login}/sample2.sh */15 * * * * sh /Users/{login}/sample3.sh The first line of the crontab file is a commented header line (indicated by a leading “#”). The specified tasks begin on the second line. Entries in columns are separated by tabs. In this example, the command in the first task line (to execute my disk mirroring script) is executed at 03:15 (i.e., 15 minutes after 3am) on every day of the week (wday) for every day (mday) of every month (month). The asterisks are read as “every”. As further examples, the second task line would execute its command at 03:15 only on the 1st day of every month. The third task line would execute its command at 03:15 on the 6th day of every week. The fourth task line would execute its command every 15 minutes. Allowed values for the crontab parameters are 0-59 for min, 0-23 for hr, 1-31 for mday, 1-12 for month, and 0-7 for wday (0 and 7 are both Sunday). The month and wday values can also be specified using the first three letters of the month or day name (e.g., Jan for January or Thu for Thursday). Ranges and (comma-separated) lists are allowed. For example, 8-11 in the hr column would run the associated command at hours 8, 9, 10, and 11. A list of 1,2,5,9 in the hr column would run the associated command at hours 1, 2, 5, and 9. Once the crontab file and the associated script file(s) have been created and stored in the specified locations, you must initiate the cron process by issuing the command crontab /Users/{login}/.crontab
(substitute your own name for your crontab file). The command crontab -r
will stop the execution of cron tasks. The command crontab -l
will list all of the currently scheduled cron tasks. I have found that cron will occasionally stop on its own (possibly associated with incremental software updates to the operating system), so it is a good idea to run crontab -l occasionally to make sure that your cron tasks are still running.
OS Version Compatibility: Lion, Snow Leopard, Leopard, Tiger
Update Status: 03 December 2009
|