Crontab
I have been having a lot of difficulty using Moveable Type in recent weeks. This has slowed progress on the website. I’ve made the switch to Wordpress which is a php alternative to the perl powered Moveable Type. I have found it to be a very nice editor that is very easy to install and maintain. While I love perl, Moveable Type was really becomming problematic for me.
Now for a little OS X scripting.
Many scripts that you run, you want to run at set intervals. Cron is the way to do that on Unix and OS X. A cron script can be run as a system root level command or it can be run as a user level command. OS X is set up to use vi as the default text editor when you edit your user level crontab. If you type this in the terminal: “crontab -e” your user cron table will come up. If you don’t have any scheduled tasks there will be a blank document. But if you have already entered some cron scripts in you will be able to use vi to change the frequency of them, or to remove or add them. You need to do a sudo vi /etc/crontab to edit the root level cron table. Here is a sample of my crontab:
30 4 * * * osascript /Volumes/Users/Admin/bin/relaunchDistiller.scpt
* * * * * perl /Volumes/Users/Admin/bin/checkClassic.plx
#* */2 * * * /Volumes/Users/Admin/bin/classicerrormail.sh
30 4 * * 7 /Volumes/Users/Admin/bin/restartClassic.sh
As you can see some of the scripts I have described in earlier entries are listed here. You can see that you can use a variety of script languages to write scripts. Cron is just the scheduler that makes them launch and execute at certain intervals. I’ve used osascript or in this case it is Applescript. I’ve also used perl and unix shell to create cron scripts.
The time intervals are space separated time slots. They are minute, hour, day of the month, month, and day of the week. My first script runs at 4:30 a.m. every day. If you wanted it to run at 4:30 in the afternoon you would use 16 30 (military time). The second script actually runs every minute. That is the most you can have a script run in cron. Since there is no definition in any of the fields, it runs every minute of every hour of every day, etc. A # is a comment, just like in perl. So line three is commented out and is not currently running. It also uses a step command which in this case would mean that it would run every two hours because it is in the hour field. The last entry runs at 4:30 a.m. but only on Sunday (1 = Monday, 7 = Sunday). The last part of the cron statement is the path to the execuatable script to be run. Make sure the permissions are set for it to be an executable file. Generally it should be set to 755.
There are a lot of pages on the web that will describe cron for you. This is a short description to get you started. And the thing to remember here is that any executable script can be run, and on OS X that even means applescript can be run. The osascript command is how you call an applescript from the command line.
Here is a good tutorial for additional learning:
June 13th, 2005 at 3:54 am
Hi, I found your interesting article about Crontab on OS X (http://www.bradrice.com/wposx/?p=51). There you have listed in your example a script “relaunchDistiller.scpt”.
I’m having trouble using Distiller running for more than a few days, so I have to restart Distiller manually.
Could you please send me your relaunch-script?
(Trying in recording the job via script-editor doesn’t succeed, opening and quitting of applications in the finder doesn’t get recorded.)
Thanks, Rolf from Germany.
July 27th, 2005 at 11:44 am
Hi
Any chance of knowing what these 2 do & getting the code?
* * * * * perl /Volumes/Users/Admin/bin/checkClassic.plx
#* */2 * * * /Volumes/Users/Admin/bin/classicerrormail.sh
I need:
30 4 * * 7 /Volumes/Users/Admin/bin/restartClassic.sh
I’m running a web app in it, and want to either check for a Classic freeze or check for a port 80 non-response prior to an automated Classic restart.
Rollo
May 4th, 2007 at 10:03 am
[…] with cron, check these resources that I found helpful: Using cron to run programs on a schedule Mac OS X and Unix tips and tricks Learning the Terminal in Jaguar *If you screw anything up, I accept no responsibility. But […]
December 9th, 2007 at 2:52 pm
[…] with cron, check these resources that I found helpful: Using cron to run programs on a schedule Mac OS X and Unix tips and tricks Learning the Terminal in Jaguar *If you screw anything up, I accept no responsibility. But […]