Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
P is for Practical
 
PerlMonks  

Cron on Mac OS X

by Ninth Prince (Acolyte)
on Sep 09, 2008 at 14:36 UTC ( #710102=perlquestion: print w/ replies, xml ) Need Help??
Ninth Prince has asked for the wisdom of the Perl Monks concerning the following question:

I posted a similar question last week, but this is not a complete re-post. Hopefully, I'm not breaking any etiquette rules.

I am a Perl novice, new to Mac, and have limited and dated experience with UNIX (15 years ago).

I am using Mac OS X 10.5. I want to execute a perl script automatically, once per day, every day. In my previous post it was suggested that I set up a file (called cron.txt and then enter crontab cron.txt in my Terminal.

Okay, so here is what I have done. In my cron.txt file I have the line 21 08 * * * /usr/bin/perl -w /Users/myname/Documents/path/to/CronTest.pl. I copied that "sixth variable" part (the part starting with /usr and all the way through /CronTest.pl) to the command line in my Terminal window and the script executed fine, so I know that command part of the cron listing is correct. (BTW, what is that "sixth variable" called? In my mind I'm calling it the command, but am not sure if this is correct.)

It was 8:19 a.m. when I saved the cron.txt file and still 8:19 when I entered crontab cron.txt. I checked crontab by entering crontab -l and it appeared to be correct. When 8:21 rolled around, however, nothing happened.

Any help would be greatly appreciated. Is there some way that I can check for errors, or see if the line is even executing?

Thanks for your help!

Comment on Cron on Mac OS X
Select or Download Code
Re: Cron on Mac OS X
by JadeNB (Chaplain) on Sep 09, 2008 at 15:02 UTC
    As Fletch mentioned in your previous post, launchd is now the 'preferred' mechanism. In particular, cron probably isn't running by default. A silly question: did you start it?

    UPDATE: Also, it seems that you asked this same question in your other thread about an hour and a half ago, so it's not really appropriate to make a fresh post here.

      Sorry about the double-posting -- I thought that had gone off somewhere else. I'm still trying to figure out how this site works.

      I did some looking into launchd and it looked much more involved than setting up a crontab. That said, I still can't get this thing to work. To answer your question, I have not turned on crontab. How would I go about doing that?

      Thanks.

        crontab itself doesn't need to be turned on as such—it's used only to tell cron to use a particular crontab file. However, cron does need to be running in order to know to do anything. I don't know about Leopard, but, on Tiger, cron is started up by launchd in response to a file called com.vix.cron.plist in /System/Library/LaunchDaemons. (That file records the path, tells it to RunAtLoad, and gives it WatchPaths and a QueueDirectory, which are /etc/crontab and /var/cron/tab, respectively.)
Re: Cron on Mac OS X
by pjotrik (Friar) on Sep 09, 2008 at 15:09 UTC
    You can check that your crontab is executed by adding a simple line like
    21 08 * * * date > /tmp/date.txt
    and then checking whether the file /tmp/date.txt was changed. Another way is to profit from the fact that cron sends an email when a task prints something to the standard output.
    MAILTO=your@mail.com 21 08 * * * date
    Beware of the fact, that your cron tasks do not have the same environment (as $PATH, for example) as your shell does. That's where problems may arise.

      I tried using MAILTO (the script does print something to STDOUT), but I received no e-mail. I'm guessing that the crontab is just not executing. It has also been suggested to me that I might need to "start" my crontab. I'm not sure what that means. Thanks.

        Crontab is a frontend program for the cron deamon - that's the one that has to be running. You can tell whether it is by doing ps aux | grep cron. On Linux the deamon is usually called crond, on a Mac I've got access to, it's simply called cron.

        On Linux (the flavors I know) you put a startup script in /etc/init.d/, on Mac this should be handled by launchd, launchctl is used for the control over this demon.

        Can't tell you much more, my knowledge of Mac is very limited. Search the web, read the docs, wait for more replies...

        Hope that helps

Re: Cron on Mac OS X
by tilly (Archbishop) on Sep 09, 2008 at 18:08 UTC
    A co-worker ran across what may be the same problem a while back. Which is that cron does not constantly check whether the crontab has been changed.

    Try setting it for a cron in a half hour or so. Or wait a day and see if it runs.

    Oh, and another warning. Cron is infamous for not starting with necessary environment variables set up. Tracking this down can be annoying. See RE (tilly) 3: Get default login environment for a solution to that problem.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://710102]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2014-04-21 10:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (492 votes), past polls