Jul. 14th, 2024
2024年 6月 9日

Post: OS X: launchd

OS X: launchd

Published 12:04 Apr 11, 2016.

Created by @ezra. Categorized in #Programming, and tagged as #macOS.

Source format: Markdown

Table of Content

I would like to create a service to execute a task automatically, and starting and stopping it on demand. In Windows, it is possible to create an application that can be installed as a service (Windows Service). So what would be the equivalent of services on Mac OS X? How are they implemented and where to start to learn about it?

Well, let's meet launchd.

You need to create launchd configuration files (in the form of a property list) that are placed in one of five locations:

  • ~/Library/LaunchAgents: Per-user agents provided by the user.
  • /Library/LaunchAgents: Per-user agents provided by the administrator.
  • /Library/LaunchDaemons: System-wide daemons provided by the administrator.
  • /System/Library/LaunchAgents: Per-user agents provided by Mac OS X.
  • /System/Library/LaunchDaemons: System-wide daemons provided by Mac OS X.

A daemon is 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.

The syntax of the configuration files is simple but it's easy to get it wrong. The Wikipedia article has a good summary of the options if the man page is not to your liking.

Essentially, what you do is install your actual command-line tool (your service) somewhere and then create a launchd configuration plist and place it in one of the above locations. You can configure the plist so that launchd runs your service at launch or periodically, or in response to various actions (such as the contents of a folder changing).


Q: The items in /Library (as opposed to a single user's ~/Library) will run for every user who logs in, correct?

A: Yes, that's right.

Q: Is there a way to have this app run as root on startup using launchd?

A: Yes, you need to use the /Library/LaunchDaemons location for your configuration file.

Q: Can you share some Mac launchd examples (also written as launchd plist examples, or launchctl examples)?

A: This launch plist script does the following things:

  • Runs a Unix shell script named /Users/al/bin/
  • Runs that script every minute, as given by the StartInterval tag.
  • Assigns the label com.devdaily.pingwebsites to this script. This is helpful when you use the launchctl command, as discussed in the earlier tutorial.
<?xml version="1.0" encoding="UTF-8"?>">
<plist version="1.0">






Pinned Message
I'm looking for a SOFTWARE PROJECT DIRECTOR / SOFTWARE R&D DIRECTOR position in a fresh and dynamic company. I would like to gain the right experience and extend my skills while working in great teams and big projects.
Feel free to contact me.
For more information, please view online résumé or download PDF
本人正在寻求任职 软件项目经理 / 软件技术经理 岗位的机会, 希望加⼊某个新鲜⽽充满活⼒的公司。
如有意向请随时 与我联系
更多信息请 查阅在线简历下载 PDF