A launchd primer covering configuration, administration and troubleshooting. and designed by Dave Zarzycki at Apple, it was introduced with Mac OS X Tiger and is licensed under the Apache License." DOCTYPE plist PUBLIC "-//Apple// DTD PLIST //EN" An OSX application is not an executable, just a directory. Several Mac OS X 'launchd' plist file examples, demonstrating different ' commands' that can be used in a launchd plist file.
These per-user processes are referred to as user agents. A user agent is essentially identical to a daemon, but is specific to a given logged-in user and executes only while that user is logged in. There are four ways to launch daemons using launchd. The preferred method is on-demand launching, but launchd can launch daemons that run continuously, and can replace inetd for launching inetd -style daemons.
In addition, launchd can start jobs at timed intervals. Although launchd supports non-launch-on-demand daemons, this use is not recommended. The launchd daemon was designed to remove the need for dependency ordering among daemons.
If you do not make your daemon be launched on demand, you will have to handle these dependencies in another way, such as by using the legacy startup item mechanism. With the introduction of launchd in OS X v What launchd provides is a harness for launching your daemon as needed.
In reality, the daemon may or may not be running. When a client sends a request to the port, launchd may have to launch the daemon so that it can handle the request. Once launched, the daemon can continue running or shut itself down to free up the memory and resources it holds.
If a daemon shuts itself down, launchd once again relaunches it as needed to process requests. In addition to the launch-on-demand feature, launchd provides the following benefits to daemon developers:.
Simplifies the process of making a daemon by handling many of the standard housekeeping chores normally associated with launching a daemon. Eliminates the primary reason for running daemons as root. Simplifies error handling and dependency management for inter-daemon communication. Because daemons launch on demand, communication requests do not fail if the daemon is not launched.
They are simply delayed until the daemon can launch and process them. After the system is booted and the kernel is running, launchd is run to finish the system initialization. As part of that initialization, it goes through the following steps:. As requests for a particular service arrive, it launches the corresponding daemon and passes the request to it.
The process for per-user agents is similar. When a user logs in, a per-user launchd is started. It does the following:.
As requests for a particular service arrive, it launches the corresponding user agent and passes the request to it. Because launchd registers the sockets and file descriptors used by all daemons before it launches any of them, daemons can be launched in any order. If a request comes in for a daemon that is not yet running, the requesting process is suspended until the target daemon finishes launching and responds.
If a daemon does not receive any requests over a specific period of time, it can choose to shut itself down and release the resources it holds.
When this happens, launchd monitors the shutdown and makes a note to launch the daemon again when future requests arrive. To run under launchd , you must provide a configuration property list file for your daemon. This file contains information about your daemon, including the list of sockets or file descriptors it uses to process requests.
Table lists the required and recommended keys for all daemons. The property list file is structured the same for both daemons and agents. You indicate whether it describes a daemon or agent by the directory you place it in. Indicates that your daemon requires a separate instance per incoming connection.
This causes launchd to behave like inetd , passing each daemon a single socket that is already connected to the incoming client. This key specifies whether your daemon launches on-demand or must always be running. It is recommended that you design your daemon to be launched on-demand. The following simple example launches a daemon named hello , passing world as a single argument, and instructs launchd to keep the job running:.
In this example, there are three keys in the top level dictionary. Setting this value in a system wide daemon will set the sysctl 3 kern. This imposes a limit on the amount of physical memory to be given to a process; if memory is tight, the system will prefer to take memory from processes that are exceeding their declared resident set size. Stack extension is performed automatically by the system. The system will apply resource limits based on what kind of job it is. The following are valid values: Background Background jobs are generally processes that do work that was not directly requested by the user.
The resource limits applied to Background jobs are intended to prevent them from disrupting the user experience. Standard Standard jobs are equivalent to no ProcessType being set. Adaptive Adaptive jobs move between the Background and Interactive classifi- cations based on activity over XPC connections. Interactive Interactive jobs run with the same resource limitations as apps, that is to say, none. Interactive jobs are critical to maintaining a responsive user experience, and this key should only be used if an app's ability to be responsive depends on it, and cannot be made Adaptive.
Setting this key to true disables that behavior. In other words, if the job cannot be safely respawned without a full machine reboot, then set this key to be true. Each key in this dictionary should be the name of service to be advertised. The value of the key must be a boolean and set to true. Alternatively, a dictionary can be used instead of a simple true value. If the value is set to true, clients receive port death notifications when the job lets go of the receive right. Setting the value to true should be done with care.
Not all clients may be able to han- dle this behavior. The default value is false. Finally, for the job itself, the values will be replaced with Mach ports at the time of check-in with launchd. OR dictionary of array of dictionaries The job must check-in to get a copy of the file descriptors using APIs outlined in launch 3. The keys of the top level Sockets dictionary can be anything.
They are meant for the application developer to use to differentiate which descriptors correspond to which application level protocols e. At check-in time, the value of each Sockets dictionary key will be an array of descriptors. The parameters below are used as inputs to call getaddrinfo 3.
The default is "stream" and other valid values for this key are "dgram" and "seqpacket" respectively. The default is true "to listen". The only value understood by this key at the moment is "TCP". It specifies the path to connect 2 or bind 2 to. Instead of binding to a known path, a securely generated socket is created and the path is assigned to the environment variable that is inherited by all jobs spawned by launchd. I can see "com. Now, how come for example sudo launchctl start com. If it's already started by launchctl there's no need to reload the plist file unless it changed as well?
It is annoying there is no restart or reload command. Anyone know of a single-command way of making for example mongod pick up a config file change? I'm just not satisfied with When I tried this my service had hung, it did not stop after a short wait, "kill" on the pid did not help, doing "kill -9" on the pid killed the process and let kickstart that had been running in another console while I tried to kill the process continue and start the service again.
I did not wait for very long before starting to kill things manually, so waiting long enough might have been enough to solve the problem. You could simply do sudo launchctl stop com. Dmitriy Apollonin Dmitriy Apollonin 4 4 bronze badges.
I tried this and now I can't use ssh from my Mac. Rebooting doesn't fix. Replacing stop with start also doesn't fix. I tried this and I still can ssh to my Mac! The question is about restarting a service, but I don't think that launchctl stop does restart a service. You are looking for launchctl. But reading the man-page does not tell me what the equivalent of either invoke-rc. I think nayden's response has got this covered.