PDA

View Full Version : Need help running a command daily with launchd


SimonDorfman.com
12-08-2005, 07:55 AM
My objective:
Run this command:

/opt/local/www/awstats/cgi-bin/awstats.pl -config=INSERT_YOUR_COMPUTER_NAME_HERE.local -update

...every day at 3am.

I saved this file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>net.sourceforge.awstats.update-daily</string>
<key>LowPriorityIO</key>
<true/>
<key>OnDemand</key>
<false/>
<key>Program</key>
<string>/opt/local/www/awstats/cgi-bin/awstats.pl</string>
<key>ProgramArguments</key>
<array>
<string>-config=INSERT_YOUR_COMPUTER_NAME_HERE.local</string>
<string>-update</string>
</array>
<key>ServiceDescription</key>
<string>Updates statistics for awstats.pl every day at 3AM.</string>
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>3</integer>
</dict>
</dict>
</plist>

...here: /Library/LaunchDaemons/net.sourceforge.awstats.update-daily.plist

But it's not running. Any idea what I'm doing wrong? Thanks very much for any help!

hayne
12-08-2005, 08:40 AM
What version of OS X are you running?
I recall reports that StartCalendarInterval was buggy until 10.4.2

SimonDorfman.com
12-08-2005, 08:48 AM
What version of OS X are you running?
I recall reports that StartCalendarInterval was buggy until 10.4.2
I'm running version 10.4.3.

hayne
12-08-2005, 09:02 AM
Is it not invoking your Perl script at all? Try replacing the Perl script with something simple that just writes "hello" to a file.

Does your Perl script work when run from the command-line?
It is rather odd to have the script in a cgi-bin directory - that's what makes me think maybe it is depending on something that comes from the web server invocation environment.

jbc
12-08-2005, 10:40 AM
I may be misunderstanding the parameter, but doesn't "OnDemand" need to have a value of "true" for launchd to run the command when scheduled?

blb
12-08-2005, 03:14 PM
Did you either run

sudo launchctl load /Library/LaunchDaemons/net.sourceforge.awstats.update-daily.plist

or reboot, so launchd picked it up?

akirwan
12-16-2005, 09:05 AM
Are StartCalendarInterval still not working in Launchd. Every time I set one up it never runs when I use that key. It is not the script I am running as that works fine if I manually run it or if I run launchctl start com.example. I have had to go back to cron to get the scripts to run. Anyone know if this is something Apple is going to fix or if there is a solution. I am running 10.4.3. Here is my plist file.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.clean_desktop</string>
<key>ProgramArguments</key>
<array>
<string>/bin/sh</string>
<string>/Library/Management/example.sh</string>
</array>
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>12</integer>
<key>Minute</key>
<integer>55</integer>
</dict>
</dict>
</plist>

jbc
12-16-2005, 12:48 PM
What version of Tiger are you running? Version 10.4.0 through version 10.4.2 had issues with launchd items not being run when expected. I believe most of these issues have been addressed in 10.4.3.

Also your ProgramArguments parameters look odd. If your shell script is written properly (with an initial shabang line "#!/bin/sh") and you've made it executable, you should *not* include the "<string>/bin/sh</string>" line ... it's unnecessary. The path to the script ("<string>/Library/Management/example.sh</string>") should be sufficient to launch it.

[edit: need more coffee...see you're running 10.4.3...sorry]

hayne
12-16-2005, 01:33 PM
akirwan:
I merged your thread in with this recent one on the same topic.

akirwan
12-16-2005, 09:49 PM
I've tried my plists with and without the /bin/sh it doesn't run either way. Also it seems to me if it was a plist issue it would not run when I type the command launchctl start com.example. My guess is that there are still problems with launchd in Tiger regarding startcalendarinterval jobs.

hayne
12-17-2005, 05:29 AM
Did you read post #6 in this thread?

blb
12-17-2005, 03:55 PM
To find out if launchd knows about a given job, use list:

$ sudo launchctl list
com.withay.hfsbackup
org.darwinports.apache2
org.opendarwin.dp.bind9
com.apple.atrun
com.apple.KernelEventAgent
com.apple.mDNSResponder
com.apple.nibindd
com.apple.periodic-daily
com.apple.periodic-monthly
com.apple.periodic-weekly
com.apple.portmap
com.apple.syslogd
com.vix.cron
org.postfix.master
org.xinetd.xinetd
com.openssh.sshd

If you don't see the job you expect, use the load command listed earlier.

akirwan
12-18-2005, 10:24 PM
Yes, the job is loaded into launchd. It is listed when I run launchctl list, but it never runs at the specified time. I have even tried turning off cron thinking there might be a conflict with launchd starcalendarinterval jobs but that did not work either.

blb
12-19-2005, 03:30 AM
The first thing I want to say is launchd's logging stinks; enabling debug-level logging still seems to keep it completely silent.

Anyway, although I do have one job that has been using the StartCalendarInterval method successfully since 10.4.3, I can't seem to get another one working with launchctl load. I hate to say it, but have you tried rebooting?

hayne
12-19-2005, 11:04 AM
Anyway, although I do have one job that has been using the StartCalendarInterval method successfully since 10.4.3, I can't seem to get another one working with launchctl load. I hate to say it, but have you tried rebooting?

I think that is definitely worth trying - after all the periodic maintenance scripts are invoked via launchd with StartCalendarInterval, and they seem to be working just fine.

akirwan
12-21-2005, 06:28 AM
I've tried rebooting and zapping the pram and a number of other trouble shooting techniques none have worked. I am hoping 10.4.4 fixes the problems with launchd.

hayne
12-21-2005, 12:26 PM
I've tried rebooting and zapping the pram and a number of other trouble shooting techniques none have worked.

Is your plist in one of the standard launchd folders?
The idea of rebooting wasn't as a troubleshooting step - it was so that your plist would get picked up as one of the jobs to be run.

As I pointed out above, the periodic maintenance scripts are run via launchd and they seem to work just fine - so it would be very surprising if you were to make a copy of one of those plists (in the same folder) and change it to run a different script and it didn't work after being rebooted.

akirwan
12-21-2005, 08:44 PM
I copied the system periodic script from /System/Library/LaunchDaemons/com.apple.periodic-weekly.plist to /Library/LaunchDaemons/. I then changed a few of the properties to run my script and I changed to times the script were suppose to run. I've run launchctl load com.example.plist nothing happens, reboot nothing nothing happens. Running launchctl list shows the specified configuration file com.example.plist. I could try installing my launchd plist file in /System/Library/LaunchDaemons/ but I know that directory is only suppose to contain plists for the System. Thanks for everyones help, I appreciate it. Just starting to get a little frustrated that I can not get startcalendarintervals to work.

hayne
12-21-2005, 09:41 PM
I could try installing my launchd plist file in /System/Library/LaunchDaemons/ but I know that directory is only suppose to contain plists for the System.

Try it anyway - this is to be considered just as a test to see what the situation is. You can delete it afterwards with no harm done.
And it would be best if you simplified your script as much as possible during this testing so you can be sure that it isn't something with your script that is causing the problem. In fact, I would recommend as a first test, just duplicating the standard daily maintenance job plist in /System/Library/LaunchDaemons/ and only changing the time that it is supposed to run. I'm almost certain that will work. Then proceed to make very small changes, testing after each step. E.g. your next step might be to make a copy of the /usr/sbin/periodic script that gets invoked by your duplicate plist - e.g. name it /usr/sbin/periodic2 and then make sure that still runs.
The next step might be to edit your /usr/sbin/periodic2 script to change it to just write the date into a file:

#!/bin/sh

dataFile=/Users/your_username/testLaunchdOutput.txt
/bin/date >> $dataFile

Does that work? Then proceed to substitute your existing script.
Once you've got your script running from this System location (and possibly after rebooting each time if using 'launchctl load' doesn't seem to work), then try moving it to /Library/LaunchDaemons/

And at least while testing, make sure your script(s) have 555 permissions like /usr/sbin/periodic. In fact, for scripts used by plists in any "LaunchDaemons" folder, it might be necessary that the scripts be owned by root and group wheel like /usr/sbin/periodic
That would seem to make sense - we don't want just anybody to be able to edit scripts that will be running as root (as they are when launched from a "LaunchDaemons" folder).

In fact, now that I think about it, I think this ownership & permissions issue is the first thing you should look into. Maybe just try fixing the ownership & permissions on your script and try again.

akirwan
01-01-2006, 11:43 PM
I started by making a copy of /System/Library/LaunchDaemons/com.apple.periodic-daily.plist to /System/Library/LaunchDaemons/com.apple.daily.plist. I then simply changed the run time and the label string to com.apple.daily. Rebooted the computer, I run sudo launchctl list and I see that com.apple.daily is listed. However the new LaunchDaemon for the daily periodic script never runs at the specified time.

mttsmnw
01-03-2006, 09:31 AM
I actually have the same problem.
I saved my job in ~/Library/LaunchAgents/ but it never got executed at the time specified.

My .plist file was also a copy of periodic-daily, modified to suit my needs. Since it never got launched I finally gave up. But a few days later it suddenly launched, but not at the time specified! (The computer was definitly running at that the time specified)

So I started to examine this behavior. And I found out, that the daily/weekly/monthly scripts are also not launched at the specified time, but at some time, which seems quite random to me:
I intentionally left my computer running over night for several days to see if daily is launched at the specified time (3:15). But it was never executed at that time.

So finally it came to the conclusion, that StartCalendarInterval is still buggy. I have no solution to this problem. I personally will switch back to cron for scheduled jobs and hope they'll fix this in 10.4.4. Also cron has some features StartCalendarInterval has not. For example: you can run a job every Monday and Wednesday or Monday till Friday or only in January and March. But in launchd you can specify only one weekday/month/... Hope they'll improve that, too.

akirwan
01-03-2006, 06:24 PM
I just noticed that my script ~/Library/LaunchAgents/ finally did run. It was set to run at 6:20PM and ran 2 days later at 2:00 am. I also noticed the odd times that the periodic scripts run, even when my computer is on they do not run at the specified times. My guess is the problem is what you said startcalendarintervals are still buggy in launchd as of 10.4.3. Hopefully Apple will fix this soon as I would really like to start using Launchd.

mttsmnw
01-04-2006, 02:22 AM
I ran another google search for the problem and found this discussion at the apple-site:
http://discussions.apple.com/thread.jspa?messageID=1206929

It says there, a job isn't launched if the time of submission is less than 1 hours away from the start time. I tested this with a small shell script, which does nothing else than write the time and date of execution to a file. And indeed: it was executed (nearly) at the right time, if the time was at least 1h in the future from the time of submission. (It was not "exactly" on time, it was about 1 minute late ...)
I'll try to examine this behavior a little further ... perhaps I can find out what's going wrong.

tjj
01-04-2006, 02:52 PM
right here (http://forums.macosxhints.com/showthread.php?p=258952#post258952)

The timers that should trigger launchd jobs appear to be buggy (still) in 10.4.3.
I can as an aside recommend lingon (http://lingon.sourceforge.net/) (no affiliation:D) to edit/create launchd jobs.
If you make a copy of apple's daily job, I would expect you would have to rename it to something unique. Otherwise I have found that either lingon or launchctl complains that "...is already loaded".

mttsmnw
01-04-2006, 04:32 PM
it seems the sleep-issue is the problem in my case. when I set up a job for 1h in the future it runs as expected. but if I set my computer to sleep for 10 minutes, it runs 10 minutes later than scheduled. that's definitly an annoying bug. I hope they'll fix this soon, I use a powerbook and need to put it to sleep many times during the day. So I'm back with cron till the next update.
btw: I already used lingon, but switched back to plist-editing and launchctl to investigate this bug (and to be sure it wasn't lingons fault, but it wasn't).