See the best Beach iPhone Backgrounds collection. If you see some Beach iPhone Backgrounds you’d like to use, just click on the image to download to your desktop or mobile devices. 20 Cool New Things in Apple OS X Yosemite. Version of the operating system already named OS Ten—is due to arrive in the fall as a free upgrade, usable on just about any Mac manufactured since.
4614 Views
4046 Views
8289 Views
1594 Views
2297 Views
4406 Views
3404 Views
1794 Views
1909 Views
814 Views
2364 Views
10791 Views
2790 Views
8045 Views
3027 Views
7786 Views
4715 Views
936 Views
5507 Views
One of the core components of Mac OS X is launchd, and it turns out it can do some cool things.
I particularly like the idea of using QueueDirectories
to monitor and act upon files dropped into a directory, without having to run any extra daemons. The files could be uploaded to S3, transcoded to a different video format, gzipped… anything.
Anyway, I recently fell into the launchd
documentation, and came out with this write-up. Let me know if you find it useful.
The first thing that the Mac OS kernel runs on boot is launchd
, which bootstraps the rest of the system by loading and managing various daemons, agents, scripts and other processes. The launchd man page clarifies the difference between a daemon and an agent:
In the launchd lexicon, a “daemon” is, by definition, a system-wide service
of which there is one instance for all clients. An “agent” is a service that
runs on a per-user basis. Daemons should not attempt to display UI or
interact directly with a user’s login session. Any and all work that involves
interacting with a user should be done through agents.
Daemons and agents are declared and configured by creating .plist
files in various locations of the system:
Perhaps best of all, launchd
is open source under the Apache License 2.0. You can currently find the latest source code on the Apple Open Source site.
The Mac OS crontab man page says:
Turns out launchd
has a simple StartInterval <integer>
property, which starts the job every N seconds. However the true cron-like power lies in StartCalendarInterval
:
Lets find the shortest example of this in action:
Better than cron? Apart from better handling of skipped jobs after system wake, it also supports per-job environment variables, which can save writing wrapper scripts around your cron jobs:
So, anything XML is obviously worse than 0 52 3 * 5 /path/to/command
, but launchd
is packing more features than cron, so it can pull it off.
Apart from having an awesome daemon/agent manager, Mac OS X also has an excellent Mail Transport Agent called postfix. There’s a good chance your ISP runs the same software to handle millions of emails every day. We’ll be using it as an example of how launchd
can start jobs based on filesystem changes.
Because your laptop isn’t, and shouldn’t be, a mail server, you don’t want postfix running all the time. But when messages are injected into it, e.g. by a script shelling out to /usr/sbin/sendmail
or /usr/bin/mail
, you want them to be delivered straight away.
Here’s how Mac OS X does it (/System/Library/LaunchDaemons/org.postfix.master.plist
):
We’ll start with the simple part. ProgramArguments
passes -e 60
to postfix, described thusly:
So postfix is told to exit after running for 60 seconds. The mystery (to me, earlier today, at least) is how it gets started. It could be on a cron-like schedule, but (a) it isn’t, (b) that would suck, and © it would result in delayed mail delivery. It turns out the magic lies in QueueDirectory
, which I initially overlooked thinking it was a postfix option. The launchd.plist man page says:
The Launchd Wikipedia page actually goes into more detail:
So launchd
can monitor a directory for new files, and then trigger an agent/daemon to consume them. In this case, the postfix sendmail(1) man page tells us that “Postfix sendmail(1) relies on the postdrop(1) command to create a queue file in the maildrop directory”, and the man page for postdrop(1) tells us that /var/spool/postfix/maildrop
is the maildrop queue. launchd
sees new mail there, fires up postfix, and then stops it after 60 seconds. This might cause deferred mail to stay deferred for quite some time, but again; your laptop isn’t a mail server.
Tranditionally the inetd and later xinetd “super-server daemon” were used to listen on various ports (e.g. FTP, telnet, …) and launch daemons on-demand to handle in-bound connection, keeping them out of memory at other times. Sounds like something launchd
could do…
Lets create a simple inetd-style server at ~/Library/LaunchAgents/my.greeter.plist
:
Load it up and give it a shot:
You can use launchd
to ensure a process stays alive forever using <key>KeepAlive</key><true/>
, or stays alive under the following conditions.
SuccessfulExit
— the previous run exited successfully (or if false, unsuccessful exit).NetworkState
— network (other than localhost) is up (or if false, down).PathState
— list of file paths exists (or if false, do not exist).OtherJobEnabled
— the other named job is enabled (or if false, disabled).These can be combined with various other properties, for example:
WorkingDirectory
EnvironmentVariables
Umask
ThrottleInterval
StartOnMount
StandardInPath
StandardOutPath
StandardErrorPath
SoftResourceLimits
and HardResourceLimits
Nice
There’s some more information at developer.apple.com, and the launchd and launchd.plist man pages are worth reading.
Let me know if you find any of this useful… I’m @pda on Twitter.
You can leave comments on Hacker News if that’s more your thing.