|
|
|
|
#1 |
|
Triple-A Player
Join Date: Aug 2004
Posts: 81
|
Periodic jobs and launchd in OS X.4
[Not completely sure which forum this should be in—apologies to the moderators if this is the wrong one…]
In Mac OS X.4, Apple moved the scheduling of the daily, weekly and monthly maintenance jobs from cron to the shiny new launchd system, as can be seen from /etc/crontab: Code:
# The periodic and atrun jobs have moved to launchd jobs # See /System/Library/LaunchDaemons # # minute hour mday month wday who command For those users who previously used e.g. anacron to make sure any overdue periodic jobs were run after starting or waking the system: below are a Perl script and a launchd job specification that may be used instead. The Perl script: Code:
#!/usr/bin/perl
#
# kick_periodic_jobs
#
# Kick off overdue daily, weekly or monthly periodic jobs
#
use Time::gmtime;
use File::stat;
$daily_logfile = "/var/log/daily.out";
$weekly_logfile = "/var/log/weekly.out";
$monthly_logfile = "/var/log/monthly.out";
# Check daily periodic job
$daily_log_age = -M $daily_logfile;
$daily_log_age = 1 if not defined $daily_log_age;
system("periodic daily") if $daily_log_age >= 1;
# Check weekly periodic job
$weekly_log_age = -M $weekly_logfile;
$weekly_log_age = 7 if not defined $weekly_log_age;
system("periodic weekly") if $weekly_log_age >= 7;
# Check monthly periodic job
$current_year = gmtime()->year;
$current_mon = gmtime()->mon;
$current_mday = gmtime()->mday;
if (-e $monthly_logfile) {
$monthly_log_year = gmtime(stat($monthly_logfile)->mtime)->year;
$monthly_log_mon = gmtime(stat($monthly_logfile)->mtime)->mon;
$monthly_log_mday = gmtime(stat($monthly_logfile)->mtime)->mday;
}
else {
$monthly_log_year = 1;
$monthly_log_mon = 1;
$monthly_log_mday = 1;
}
system("periodic monthly") if $monthly_year < $current_year && $monthly_log_mon < $current_mon && $monthly_log_mday <= $current_mday;
#EOF
If you save it elsewhere, also make sure to enter the correct path to the script in the ProgramArguments key in the launchd job specification. The launchd job specification should be saved in /Library/LaunchDaemons as e.g. local.periodic.kick.plist, with owner/group set to root/wheel and rw-r--r-- as permissions: Code:
<?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>local.periodic.kick</string>
<key>UserName</key>
<string>root</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/kick_periodic_jobs</string>
</array>
<key>StartInterval</key>
<integer>600</integer>
</dict>
</plist>
Code:
$ sudo launchctl load <path of job plist file> Code:
$ sudo launchctl list Ciao, Roeland. |
|
|
|
|
|
#2 |
|
Triple-A Player
Join Date: Jan 2002
Location: Hamburg; Germany
Posts: 241
|
Thanks for pointing that one out!
However, I read in another forum that launchd's "StartInterval" option to schedule jobs at a certain interval, does not behave like anacron. If launchd "misses" a task because the machine was shut down at the time it will NOT run the task. Anacron will. So if this is really true, is anacron still the way to solve that problem or are there any other means? |
|
|
|
|
|
#3 |
|
Triple-A Player
Join Date: Aug 2004
Posts: 81
|
In general, anacron would still be preferred to run any missed jobs. As you point out, there is no such functionality in launchd (yet).
In a specific case of e.g. the standard periodic jobs, you could also implement anacron's behaviour with launchd and a frequently run helper script. The above is just an example—you could have done the same with good old cron, but it was a nice small excercise to learn something about the new launchd functionality.
|
|
|
|
|
|
#4 | |||||||||||||||||||
|
Moderator
Join Date: Jan 2002
Location: Montreal
Posts: 29,278
|
I just saw the following (from an Apple employee) on the macosx-admins mailing list:
|
|||||||||||||||||||
|
|
|
|
|
#5 |
|
Triple-A Player
Join Date: Aug 2004
Posts: 81
|
Thanks for the interesting info, Hayne. Let's hope the fixes make it into the first update.
|
|
|
|
|
|
#6 |
|
Major Leaguer
Join Date: Dec 2002
Location: Copenhagen, Denmark
Posts: 271
|
It appears that this/these bugs are still unquashed in 10.4.3.
I have a single launchd item running which does not fire more than once when the startcalendarinterval is set, and, if the trigger is the startinterval, it works as designed except that waking from sleep the time my powerbook has slept is presumably added to the timer. ![]() Anyone with other experience? And thanks to hayne for digging out this info.
__________________
-- Thomas 10.4.11 on Pismo 500 MHz/1GB 10.5.6 on iMac intel 2.16 GHz core duo/2GB |
|
|
|
|
|
#7 |
|
Major Leaguer
Join Date: Dec 2002
Location: Copenhagen, Denmark
Posts: 271
|
hrmm,
found this thread at discussions.apple. launchd appears not to work as advertized (yet) in the man pages. This is consistent with the behavior observed here. Happy New Year
__________________
-- Thomas 10.4.11 on Pismo 500 MHz/1GB 10.5.6 on iMac intel 2.16 GHz core duo/2GB |
|
|
|
|
|
#8 |
|
Registered User
Join Date: Aug 2006
Posts: 1
|
The kicker script is a neat solution. Thanks!
I would like to suggest a change in the last part of the script. That way the monthly job will run when expected. Darelons last lines: Code:
system("periodic monthly") if $monthly_year < $current_year && $monthly_log_mon < $current_mon && $monthly_log_mday <= $current_mday;
#EOF
Code:
$monthly_log_mon += -12 if $monthly_log_year < $current_year;
system("periodic monthly") if $monthly_log_mon < $current_mon && $monthly_log_mday <= $current_mday;
#EOF
Last edited by carol; 08-21-2006 at 01:01 PM. Reason: Sentence about crontab is confusing. |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|