Re: Running a perl script automatically on Mac OS X 10.5
by tilly (Archbishop) on Sep 05, 2008 at 15:35 UTC
|
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
| [reply] [d/l] [select] |
|
|
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?
| [reply] [d/l] [select] |
|
|
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...
| [reply] [d/l] [select] |
|
|
|
|
|
|
|
|
|
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. | [reply] [d/l] [select] |
Re: Running a perl script automatically on Mac OS X 10.5
by moritz (Cardinal) on Sep 05, 2008 at 15:33 UTC
|
On Unix systems the common practice is to use cron, and since Mac OS X is Unix based I would guess it's also available on the Mac.
If it is, that would be the way I recommend. | [reply] |
|
|
Cron does exist, but launchd is the "preferred" mechanism.
Automator's probably barking up the wrong tree; that's more of a point-and-drool interface for setting up AppleScript-able events (not that AppleScript wasn't an attempted "ANYONE R PROGRAMR" interface; if that's what you're after though consider Mac::Glue instead).
The cake is a lie.
The cake is a lie.
The cake is a lie.
| [reply] |
Re: Running a perl script automatically on Mac OS X 10.5
by Your Mother (Archbishop) on Sep 05, 2008 at 18:01 UTC
|
Since you're not a formal programmer tilly's advice above might be better but you don't need an intermediate file stage to work with the cron tools. The -e flag starts an editor on the current, or a scratch, crontab. I edit mine on OS X (every version since the public beta has had cron) this way-
crontab -e
And to see what your crontab currently looks like-
crontab -l
To see what editor will be used try-
moo@cow[1]~>env | grep EDITOR
EDITOR=emacs
Proably shouldn't mess with it if you're not familiar with whatever name pops up.
| [reply] [d/l] [select] |
|
|
I avoid -e for the simple reason that I really like having my crontab documented in a file that is under source control.
| [reply] |
|
|
Indeed. You may have converted me just by saying it. I started putting my own utility scripts and such for various sites, checks, updates into revision control only recently. I've long done it for lib files but a month or two ago started getting all the incidentals I take for granted, like crontabs, in there too. Never again will a trashed hard drive cost me more than the money to replace it and the time to restore it.
| [reply] |
|
|
| [reply] |
Re: Running a perl script automatically on Mac OS X 10.5
by dwm042 (Priest) on Sep 05, 2008 at 16:21 UTC
|
Not only is cron your friend in the Unix world, there is also the at command. The 'at' command allows you to schedule a command to run once. An at man page is here. | [reply] |
|
|
| [reply] |
|
|
Not only is cron your friend in the Unix world, there is also the at command.
For completeness, it has to be said that at used to rely on an atrun command scheduled to run under cron every ten minutes, IIRC: in modern implementations it is based on an atd daemon itself instead, and atrun is a convenient script left there for compatibility.
More interestingly, it has to be said that PC's as commonly used tend not to be turned on 24/7, and thus cron (or whatever) scheduling should be tailored on the user's habits. Alternatively, I know there to exist an anacron utility, which:
is a computer program that performs periodic command scheduling which is traditionally done by cron, but without assuming that the system is running continuously. Thus, it can be used to control the execution of daily, weekly, and monthly jobs (or anything with a period of n days), on systems that don't run 24 hours a day. Anacron was originally conceived and implemented by Christian Schwarz in Perl, for the Unix operating system. The current implementation, in C, was written by Itai Tzur and is actively maintained by Sean 'Shaleh' Perry.
Unfortunately, AIUI it is not much known nor used, especially by major Linux distros, which often schedule "housekeeping" operations at times at which the computer will likely be off, instead. Incidentally and in the same vein as above, anacron is not a daemon but must be scheduled with cron - a design which I personally find entirely reasonable.
(Sorry for replying so late!)
| [reply] [d/l] [select] |
Re: Running a perl script automatically on Mac OS X 10.5
by broomduster (Priest) on Sep 05, 2008 at 15:35 UTC
|
I don't use Automator, but as I understand things it is for automating "work flows" and not for scheduling. You can use iCal for scheduling or you can use cron.
I use cron for this kind of thing. I have never used iCal events to run programs, but some people apparently do it that way. | [reply] |