in reply to Running a perl script automatically on Mac OS X 10.5

Under the hood OS X is Unix, so you can just use cron. Create a text file called cron.txt that looks something like this:
23 05 * * * /Users/your_name/bin/perl-script and arguments
then in a command shell type:
crontab cron.txt
Your command should now run at 5:23 AM every day.

For more details check the following man pages from your command shell:

man cron man crontab man 5 crontab # This explains the layout of the cron file

Replies are listed 'Best First'.
Re^2: Running a perl script automatically on Mac OS X 10.5
by Ninth Prince (Acolyte) on Sep 05, 2008 at 18:44 UTC

    Thanks for the quick response.

    I'm having a bit of trouble and I think it's just related to my ignorance of UNIX. I created the text file, cron.txt as you suggested. I then did the whole "crontab cron.txt" thing. My question is, after the first 5 variables are declared (Minute, Hour, etc.), what is the "sixth" variable exactly? I thought it was the command that I would execute if I was at the command prompt. So, for me, I put in something like perl -w ~/path/to/script.pl.

    I guess I should put the whole thing

    23 13 * * * perl -w ~/path/to/script.pl

    But, it doesn't seem to execute. What to do?

      Hi, I don't know much about OS X, but some OS have additional security restrictions. E.g., your UID might need to be listed in /etc/cron.allow and not in /etc/cron.deny (ALL matches every user) to be eligible to execute cronjobs.

      Usually cronjobs are executed under plain /bin/sh, so you might explicitly want to change that. Furthermore, I would not trust tilde expansion. It might work, but using $HOME/path/to/script.pl or even manually expanding ~/ might help here. Most cron systems allow to set SHELL and HOME in the cronjob file.

      Perl should be in your $PATH, but expanding that to /usr/bin/perl - or wherever perl is installed on your system is more reliable and secure. If your script.pl accesses some non-standard environment variables (i.e., via %ENV) you might need to explicitly start your standard shell (see: /etc/passwd) or use a wrapper.

      So, you might end up with: 23 13 * * * /usr/bin/perl -w /home/prince9/path/to/script.pl
      where /home/prince9 is the output of echo ~ and /usr/bin/perl is hopefully the output of type perl ... have a look at e.g. /var/log/messages too...

        Had to go away for a few days. Okay, so here is what I have done. In my cron.txt file I now 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.

        If you think this is a security issue, could you be a bit more specific about what I might have to do (My exposure to UNIX is limited and occurred about 15 years ago.)

        If not a security issue, what might I be doing wrong?

        Thanks for your help!

      Cron jobs tend to run in a less "rich" environment, so you do need to be more explicit about things like paths. In particular, cron on (BSD-based) macosx uses "sh" instead of "bash", which means that the tilde is treated as just a tilde, not a special abbreviation for $HOME. However, cron will start you out in your home directory, so instead of tilde, you can just use period. Also, your PATH will be limited to "trusted" directories (not including ".") If your perl script begins with the standard shebang line:
      #!/usr/bin/perl
      and if the current "mode flags" on the script file allow it to be executable (use the chmod +x to make the script executable), then your crontab entry can simply be:
      23 13 * * * /absolute/path/to/script.pl
      (plus any args that the script might need for its @ARGV). Note that if the script writes anything to STDOUT or STDERR, this material will be sent to your mac login account as an email message (check out the unix "mail" command), unless your cron entry includes redirection on the command line, e.g.:
      23 13 * * * ./mypath/to/script.pl > cron.out 2> cron.err
      In that example, "./" represents your home directory, which is where the output files will be created.