Mac OSX is essentially a UNIX distribution, so we have all the cool under-the-bonnet tricks and tools of any operating system with a UNIX backbone. One of the utilities I’ve always found useful in a UNIX OS is the CRON tab:
“Cron is a time-based job scheduler in Unix-like computer operating systems. Cron enables users to schedule jobs (commands or shell scripts) to run periodically at certain times or dates. It is commonly used to automate system maintenance or administration, though its general-purpose nature means that it can be used for other purposes, such as connecting to the Internet and downloading email.”
To set up a new Cron job, open your Terminal and type:
sudo crontab -e
You’ll be prompted for your password, and upon correct submission, will open a blank VI window.
The basic format of a Cron is:
0 0 * * * sh /directory/script.sh
1. The first 0 denotes minutes past the hour (0 is on the hour)
2. The second 0 denotes the hour of the day (0 is 12:00am)
3. The first * denotes day of the week (* means every day, 1 is Monday etc)
4. The second * is the week of the month (* means every week, 1 is the first week of the month)
5. The final * is the month of the year (* means every month, 1 is the first month of the year)
We specify the type of script we wish to execute; in the example above, we’re executing a basic shell script, and the final parameter details the absolute location of the script you wish to execute at regular intervals.
I often execute daily PHP scripts on my server, an example of a PHP script (backup.php) running every day at 6pm is:
0 18 * * * php ~/site/cleanup.php
Note the script type is now php, as we’re executing a php script, not a shell script.
Finally, let’s write a script that backs up my ssh keys from ~/.ssh to my Dropbox directory, once a week, on a Monday at 9:30am.
Using Terminal.app, open a new file for editing anywhere on your system (I use vi).
sudo vi ~/src/backup.sh
Copy and paste the script below, write to backup.sh and exit vi.
#!/bin/bash
sudo rsync -av --progress --delete --log-file=/Users/*yourmachine*/logs/$(date +%Y%m%d)_rsync.log /Users/*yourmachine*/.ssh /Users/*yourmachine*/Dropbox
The script uses rsync to backup the directory at “/Users/*yourmachine*/.ssh” to your Dropbox directory at “/Users/*yourmachine*/Dropbox”. It’ll also log the backup in a logs directory in your home directory. Now for the Cron.
30 9 1 * * sh ~/src/backup.sh
Hopefully this will get you started with using Cron on your OSX system.
“Cron is a time-based job scheduler in Unix-like computer operating systems. Cron enables users to schedule jobs (commands or shell scripts) to run periodically at certain times or dates. It is commonly used to automate system maintenance or administration, though its general-purpose nature means that it can be used for other purposes, such as connecting to the Internet and downloading email.”
To set up a new Cron job, open your Terminal and type:
sudo crontab -e
You’ll be prompted for your password, and upon correct submission, will open a blank VI window.
The basic format of a Cron is:
0 0 * * * sh /directory/script.sh
1. The first 0 denotes minutes past the hour (0 is on the hour)
2. The second 0 denotes the hour of the day (0 is 12:00am)
3. The first * denotes day of the week (* means every day, 1 is Monday etc)
4. The second * is the week of the month (* means every week, 1 is the first week of the month)
5. The final * is the month of the year (* means every month, 1 is the first month of the year)
We specify the type of script we wish to execute; in the example above, we’re executing a basic shell script, and the final parameter details the absolute location of the script you wish to execute at regular intervals.
I often execute daily PHP scripts on my server, an example of a PHP script (backup.php) running every day at 6pm is:
0 18 * * * php ~/site/cleanup.php
Note the script type is now php, as we’re executing a php script, not a shell script.
Finally, let’s write a script that backs up my ssh keys from ~/.ssh to my Dropbox directory, once a week, on a Monday at 9:30am.
Using Terminal.app, open a new file for editing anywhere on your system (I use vi).
sudo vi ~/src/backup.sh
Copy and paste the script below, write to backup.sh and exit vi.
#!/bin/bash
sudo rsync -av --progress --delete --log-file=/Users/*yourmachine*/logs/$(date +%Y%m%d)_rsync.log /Users/*yourmachine*/.ssh /Users/*yourmachine*/Dropbox
The script uses rsync to backup the directory at “/Users/*yourmachine*/.ssh” to your Dropbox directory at “/Users/*yourmachine*/Dropbox”. It’ll also log the backup in a logs directory in your home directory. Now for the Cron.
30 9 1 * * sh ~/src/backup.sh
Hopefully this will get you started with using Cron on your OSX system.
No comments:
Post a Comment