Help with cron
August 3, 2007 7:41 PM   RSS feed for this thread Subscribe

How do I make a command-line script run at startup/login in OSX?

I want to make rkhunter run while I'm asleep. I've found a script to get it to update, run, and write to a temp logfile, but this howto just tells me to run it as a cron job. I'm trying to figure out cron, but that tutorial tells me to run sudo cron as a startup script. That's what I want to use cron for!

What do I have to do to get the cron daemon going every time I log in? Both short answers and links to long, way-too-much-information texts are welcome.
posted by tylermoody to technology (9 comments total)
launchd starts at boot.
posted by rhizome at 8:04 PM on August 3, 2007


I may be misunderstanding you: are you trying to schedule a script to run at a certain time, or get it to run as soon as you log in?
posted by John Kenneth Fisher at 8:06 PM on August 3, 2007


(If you just want to use cron to schedule it to run at certain times, it's already set up for you, you'll just need to tell it what to run and when. I recommend Cronnix. I KNOW how to do it terminal way, and I still use cronnix.)
posted by John Kenneth Fisher at 8:08 PM on August 3, 2007


The canonical method of starting processes at boot time in Mac OS X is launchd (see also "man launchd"). It can also act as a replacement for cron (using the StartInterval or StartCalendarInterval keys in the plist file). Details on the plist format for launchd can be read using " man launchd.plist".
posted by RichardP at 8:10 PM on August 3, 2007


(If you just want it to run on login, you can also use a login hook , which is far from the most elegant way to do it, but it is simple and it works, both of which are desirable.)
posted by John Kenneth Fisher at 8:12 PM on August 3, 2007


On my Macs, I have found that the cron daemon runs by default. I use it for all sorts of things, and don't recall having had to start it. To test that it is running, try a:

crontab -l

It will list the cron jobs you have scheduled, which is probably blank.

To get the cron service to run your script, you have to add it to the crontab file. If you are working from the command line, the command to do that is:

crontab -e

The format of the crontab file is a little odd, but is documented if you type:

man crontab

in the terminal.

I would test it out with a simple script that does something easy to make sure it is working, like:

#!/bin/csh
mail -s "Crontab ran" my@emailaddress.com


You probably need to put that in a file, then make it executable by doing:

chmod +x testscript

I would then run it by hand:

./testscript

to make sure it worked, then add it to cron with the crontab -e as above. You should receive mail from it.

Once that was working, write up the other script you want, test it by hand, and then add it to the crontab once you knew it worked.

It isn't as awful as I have made it sound, once you work it all out. :) Good luck.
posted by procrastination at 8:12 PM on August 3, 2007


Lingon is an excellent GUI interface to launchd. (Click on Assistant in the toolbar and let it walk you through the steps.)

You can also use cron, or create an item in /LibraryStartupItems, but both methods are effectively superceded by launchd in 10.4+.
posted by Armitage Shanks at 9:15 PM on August 3, 2007


Btw, if you look at the System Daemons tab in Lingon, you'll see the launchd item that starts up cron at boot.
posted by Armitage Shanks at 9:30 PM on August 3, 2007


Cron is the right way to run a script in the background at a specified date and time when the computer is running. It's not the right way to tell the computer to run a script at login/startup time. Which one do you want to do?

By the way, cron is already installed and running in your OS X system; you needn't do anything extra to activate it.
posted by ikkyu2 at 1:45 PM on August 4, 2007


« Older Several years ago I came acros...   |   How did the word "Brave&q... Newer »
This thread is closed to new comments.