How do you make a script or app to run or stop running automatically when the system is booted, a user logs on, at a specific time, when a disk is attached, etc.?
MacOS, Startup – Running, Stopping, Relaunching Applications Automatically at Boot or Login
bootlogin-itemsmacosstartup
Related Question
- How to run a script on login whilst being hidden
- MacOS – How to prevent Music app from starting automatically randomly
- MacOS – FileVault – One account can unlock but preventing full login and forcing logout and login again with other user
- MacOS – Unmount Volume on User Logout
- How to Run an Installed Application as Admin on macOS
Best Answer
Gui Method
If you want something to run when a user logs in, the easiest way is to use the GUI. You can
System Prefs > Accounts > Login Items
screen, and add the item there by+
symbol.app
onto the paneOptions
, then selectOpen at Login
Launchd method
If you want to launch something that is not a
.app
, or you want to have more control over launching it, such as:then you want to use the technical solution that is
launchd
.The easiest way to set up a
launchd
config file is to use a tool to help. At the time of writing the most popular method is Lingon (which has an older free version available from SourceForge), or the PList Website tool.Hand Coding
If you don't want to use Lingon or the PList Website and only want to use the raw tools available on the system, you can write your own
launchd
plist
by handHere is my launchd script to run SomeApp continuously after the system boots (independent of a user logging in). It is in /System/Library/LaunchAgents/ and called SomeApp.restart.plist. If it is run based on a user login, it could be stored in ~/Library/LaunchAgents/
Load it once with
RunAtLoad
option will launch the application the first time launchctl runs this.KeepAlive
option will re-launch the application if it crashes.StartCalendarInterval
will run it at a certain time. Presumably this is mutually exclusive with the previous two, but is included to show what can be done withlaunchd
There are many additional options that can be found by experimenting with Lingon or by reading the man pages for
launchctl
,launchd.plist
,plist
,launchd.conf
, etc.