Kickstarting and tearing down with launchctl

At times, the names of command tools and services may seem random, and the tasks they perform appear far-flung and disparate. In fact, there are some conventions on naming which are generally respected: names ending in -d are normally background services or daemons, -ctl is a tool which controls a -d or other service, and -util are tools to work in different domains.

Knowing that, it’s not hard to guess that launchctl is the control tool to accompany the launchd service, which is second only after the kernel itself in importance to macOS. It’s responsible for launching services and other processes. This is in the sense of Launch Daemons rather than the Finder’s Launch Services: the latter is about managing GUI apps at the front end, rather than what’s going on in the engineroom.

launchd and launchctl are relatively recent, and innovations in Mac OS X. They first appeared in 10.4, and have their origins in mach_init and init in the original Mach and BSD subsystems which made up Mac OS X. They’ve since been ported to other forms of Unix too, and have grown in power and importance. Once macOS has gone through the firmware phase of booting and its kernel is started up, launchd is the next process to be started, so always has a process ID of 1. If launchd ever crashes, then that is guaranteed to panic the kernel, as it’s so important.

launchctl isn’t a tool for the curious or experimental. What it does tends to be serious, and if you’re not cautious you can bring macOS down around you. But it’s also invaluable at times: in this article I look at two of its subcommands: kickstart, whch can be used to run a service immediately, and reboot, which you can use to tear down what’s running.

launchctl kickstart

There are times when you want to run a particular service immediately, either to replace an existing service (typically in Launch Daemons) or one that isn’t set to run otherwise. Use
launchctl kickstart [option] target
where [option] can be -k to kill the running service before restarting it, or -p to return the process ID of the service as its output. The target can be specified as any of
system/name to run name in the system domain
user/uid/name to run name for the given UID, e.g. 501
and others you’ll find detailed in the man page.

So
launchctl kickstart -k user/501/cfprefsd
would kill the current user copy of cfprefsd which is running and start a new copy.

launchctl reboot

These can be among the most useful commands for tearing down userspace and bringing it back up, which can save you restarting your Mac. These can be much faster than GUI command equivalents, but you must be careful when using them, as any unsaved data is likely to be lost. Some may require elevated privileges using sudo.

launchctl reboot or launchctl reboot system
forces all open apps, services, etc., to quit immediately, then restarts your Mac as if a reboot command had been given. Beware of the first of these: if you intend typing in another subcommand after reboot, don’t accidentally press return, or you’ll find your Mac suddenly restarting!

launchctl reboot halt
does the same teardown of userspace, but shuts down instead of rebooting at the end.

launchctl reboot userspace
leaves the system running, but tears down userspace, forcing all open apps to quit, then brings userspace back up again. It’s somewhere between logging out and restarting, and relies on the underlying system continuing to run.

launchctl reboot logout
is similar to the GUI command to log the user out, but is much quicker as apps are quit immediately, without any option to save open files, etc.

launchctl reboot apps
terminates all the apps you are running, except those such as the Finder and Dock, leaving those, LaunchServices etc. still running. This too doesn’t give the apps any chance to save open files, but is an excellent way of quitting all open apps very quickly.

launchctl has many other subcommands, some which are legacy features of greater use in the past, and others which are very complex, such as config, which sets persistent configuration information for services. However, kickstart and reboot are two that aren’t well known, and can be very useful.