TL;DR
- You typically want to use
launchctl load -w
and launchctl unload -w
.
start
and stop
are usually reserved for testing or debugging a job.
Details
launchctl start <label>
: Starts the job. This is usually reserved just for testing or debugging a particular job.
launchctl stop <label>
: Stops the job. Opposite of start, and it's possible that the job will immediately restart if the job is configured to stay running.
launchctl remove <label>
: Removes the job from launchd
, but asynchronously. It will not wait for the job to actually stop before returning, so no error handling on this one.
launchctl load <path>
: Loads and starts the job as long as the job is not "disabled."
launchctl unload <path>
: Stops and unloads the job. The job will still restart on the next login/reboot.
launchctl load -w <path>
: Loads and starts the job while also marking the job as "not disabled." The job will restart on the next login/reboot.
launchctl unload -w <path>
: Stops and unloads and disables the job. The job will NOT restart on the next login/restart.
Where do I find the job label for a daemon, is it in the plist file?
Yes, it's in the plist file and it typically matches the filename of the plist file.
Launchd and Folder Actions are two vastly different concepts.
Folder Actions
Folder Actions allow you to attach actions in the form of Apple Scripts folders which get triggered if the content of the folder changes. Typical use cases are e.g.
- a folder to auto-upload photos to a website
- removal of file attributes (e.g. the "downloaded" attribute)
- triggering import into Evernote
- etc.
launchd
launchd is a system-wide feature to run and control system-level background tasks. These can be either running all the time or get triggered by specific events (e.g. a connection request coming in over IP, a changing folder, a specific point in time etc.). OS X makes heavy use of this concept for all kind of things, have a look at the names in /Library/LaunchAgents
and /Library/LaunchDaemons
to get an idea of functionality depending on launchd.
Because launchd is a system service running in the background all the time it can't be accessed by the user directly. To send commands to launchd, the launchctl
command is used.
Answers to specific questions
- Do folder actions use launchd?
Most probably not. You can use launchd to do everything folder actions can do but at least on a level visibile to the user these are two different concepts
- Is one more efficient than the other?
No difference here, both get triggered by OS X upon special conditions.
- Is one more powerful than the other?
launchd is definitively more powerful.
- Is one used more over the other?
In a newly installed system there already is a great number of active launchd scripts but no folder actions. Because the later must be explicitely activated and configured by the user, it's probably a safe guess that launchd is used more often.
Best Answer
launchd does a lot more than cron. In addition to running programs at certain times, it can also run them based on other triggers: incoming network connection, when a file is added to a certain folder, etc. It can also relaunch a program if it crashes.
If you're interested in it specifically as a replacement for cron, the main difference is how it handles being asleep or shut down when a job is scheduled to run. In this case, cron simply doesn't run the job, but launchd will notice that a scheduled run has been missed, and run it at the next opportunity (although if it's missed multiple scheduled runs, it'll only run it once).
Edit: I should also point out some things that cron can do that launchd cannot: automatic emailing of failure reports (you'd have to write a wrapper script to do this with launchd) and user-defined jobs that run even when the user isn't logged in (a user can define launchd items in ~/Library/LaunchAgents, but they only run inside a login session).