- I bought a book on OS X and it says that launchd now control the scheduling of cron tasks, it this true?
Yes, It's true, According to the Scheduling Timed Jobs in the Daemons and Services Programming Guide. The periodic
jobs are deprecated and using launchd
is the preferred way to management scheduling tasks rather than cron
.
In fact, Both periodic
and cron
are managed by the launchd
, As you can see below or described in manpages of periodic(8)
and cron(8)
.
$ sudo launchctl list | grep 'periodic\|cron'
6706 0 com.apple.systemstats.microstackshot_periodic
- 0 com.apple.periodic-monthly
- 0 com.apple.periodic-weekly
21057 0 com.apple.periodic-daily
- 0 com.vix.cron
$ cat com.vix.cron.plist
<?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.vix.cron</string>
<key>ProgramArguments</key>
<array>
<string>/usr/sbin/cron</string>
</array>
<key>KeepAlive</key>
<dict>
<key>PathState</key>
<dict>
<key>/etc/crontab</key>
<true/>
</dict>
</dict>
<key>QueueDirectories</key>
<array>
<string>/usr/lib/cron/tabs</string>
</array>
<key>EnableTransactions</key>
<true/>
</dict>
</plist>
- Also it says that if I place commands in /etc/daily.local that they will execute after the scripts in /etc/periodic/daily.
In my /etc/daily.local script, I just have a simple touch command (which just creates a file) so I can test to see if it works.
I manually ran
sudo /usr/sbin/periodic daily
to force it to execute. The file was never created.
I tested it and it works on my machine which is macOS Catalina.
The /etc/daily.local
file is defined in /etc/defaults/periodic.conf
file and get executed by /etc/periodic/daily/999.local
script. So make sure you have both files there.
$ cat /etc/periodic/daily/999.local
#!/bin/sh
#
# $FreeBSD: src/etc/periodic/daily/999.local,v 1.5 2001/06/01 16:40:55 dougb Exp $
#
# Run the old /etc/daily.local script. This is really for backwards
# compatibility more than anything else.
#
# If there is a global system configuration file, suck it in.
#
if [ -r /etc/defaults/periodic.conf ]
then
. /etc/defaults/periodic.conf
source_periodic_confs
fi
rc=0
for script in $daily_local
do
echo ''
case "$script" in
/*)
if [ -f "$script" ]
then
echo "Running $script:"
sh $script || rc=3
else
echo "$script: No such file"
[ $rc -lt 2 ] && rc=2
fi;;
*)
echo "$script: Not an absolute path"
[ $rc -lt 2 ] && rc=2;;
esac
done
exit $rc
- Where did I got wrong, or misunderstand anything? How should I be scheduling cron jobs in OSX Lion? Why did my book tell me to do it this way?
Althrough it had good and short answer on
https://superuser.com/a/391214/106136 by @kyle-jones, I am going to give the example in the launchd
way here, Check it here for more details.
The following property list runs the program happybirthday
at midnight every time July 11 falls on a Sunday, Save it as 'foo.plist' and run it with command launchctl load /path/to/plist
.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.example.happybirthday</string>
<key>ProgramArguments</key>
<array>
<string>happybirthday</string>
</array>
<key>StartCalendarInterval</key>
<dict>
<key>Day</key>
<integer>11</integer>
<key>Hour</key>
<integer>0</integer>
<key>Minute</key>
<integer>0</integer>
<key>Month</key>
<integer>7</integer>
<key>Weekday</key>
<integer>0</integer>
</dict>
</dict>
</plist>
I'm starting to think periodic
is different from cron
. What is /usr/sbin/periodic
?
The periodic
is a utility that written in shell script, which organized scripts in separated folders named daily
, weekly
and monthly
, and is intended to be called by launchd(8)
to execute shell scripts located in the specified directory at the right time.
$ file `which periodic`
/usr/sbin/periodic: POSIX shell script text executable, ASCII text
$ tree -Ca /etc/periodic/
/etc/periodic/
|-- daily
| |-- 110.clean-tmps
| |-- 130.clean-msgs
| |-- 140.clean-rwho
| |-- 199.clean-fax
| |-- 310.accounting
| |-- 400.status-disks
| |-- 420.status-network
| |-- 430.status-rwho
| `-- 999.local
|-- monthly
| |-- 199.rotate-fax
| |-- 200.accounting
| `-- 999.local
`-- weekly
`-- 999.local