Skip to main content

launchctl

How to prevent a service to start at login

List services configured to start at login

ls /Library/LaunchDaemons

Remove a service

sudo launchctl unload /Library/LaunchDaemons/com.example.service.plist
sudo rm /Library/LaunchDaemons/com.example.multipass.plist

Repeat for LaunchAgents

launchctl unload ~/Library/LaunchAgents/com.example.service.plist
rm ~/Library/LaunchAgents/com.example.service.plist

About launchctl

launchctl`` control Apple's launchdmanager for launch daemons (system-wide services) and launch agents (per-user programs).launchdloads XML-based*.plist` files placed in the appropriate locations, and runs the corresponding commands according to their defined schedule. More information: https://manned.org/launchctl.

launchd is a unified, open-source service management framework for starting, stopping and managing daemons, applications, processes, and scripts on macOS systems. It is essentially the macOS version of systemd or init on Linux.

launchd is commonly used for:

  • Running jobs on a schedule (like cron jobs in Unix).
  • Starting and managing background services.
  • Launching apps at startup.
  • Restarting crashed services.
  • Managing system and application tasks that need to occur at certain points, such as at load time, on demand, or at regular intervals.

Usage

Activate a user-specific agent to be loaded into launchd whenever the user logs in:

  launchctl load ~/Library/LaunchAgents/my_script.plist

Activate an agent which requires root privileges to run and/or should be loaded whenever any user logs in (note the absence of ~ in the path):

  sudo launchctl load /Library/LaunchAgents/root_script.plist

Activate a system-wide daemon to be loaded whenever the system boots up (even if no user logs in):

  sudo launchctl load /Library/LaunchDaemons/system_daemon.plist

Show all loaded agents/daemons, with the PID if the process they specify is currently running, and the exit code returned the last time they ran:

  launchctl list

Unload a currently loaded agent, e.g. to make changes (note: the plist file is automatically loaded into launchd after a reboot and/or logging in):

  launchctl unload ~/Library/LaunchAgents/my_script.plist

Manually run a known (loaded) agent/daemon, even if it is not the right time (note: this command uses the agent's label, rather than the filename):

  launchctl start script_file

Manually kill the process associated with a known agent/daemon, if it is running:

  launchctl stop script_file

About LaunchAgents and LaunchDaemons

On macOS, both LaunchAgents and LaunchDaemons are used to run and manage background processes.

  • LaunchDaemons: These are system-level services that are started by the system, and they run as root or as any user. They are not associated with a graphical user interface, and they start regardless of whether a user is logged into the system or not. They are typically used for lower-level system services.
  • LaunchAgents: These are user-level services that are started on behalf of a user and run with the user's permissions. They are often associated with applications that have a graphical user interface and they only run when a user is logged in. They are typically used for user-level tasks and applications.