Automating Login and Startup Events in macOS

Learn how to use the Automator app and launchd to control automated startup and login actions on macOS.

Mac Startup

There are a variety of reasons you might need to run a process or script at startup or login on a Mac. Most recently, I needed to get a freshly spun-up Mac VM to register itself with a third party at startup. Whatever your use case, there are three clear paths to executing an action at startup or login on macOS.

The first, using the Automator app, will appeal to folks who feel most at home in the GUI, and the second two involve creating XML files and placing them in certain directories, the contents of which will be executed at either machine startup or user login.

Option 1: Using the Automator App

Open your Applications directory in the Finder, and double click on the Automator icon. Then, select “Application,” like so:

Applications Directory_Automator Icon

Then in the following view, you will need to click Utilities in the leftmost menu and then double click Run Shell Script. Add the command you would like to run in the view that appears, like so:

Run Shell Script

Finally, save the application to the Desktop, where you will see the Automator app icon.

Next, you will need to navigate to System Preferences > Users & Groups. Select Login Items and then drag and drop your new application to add it to your login items for the selected user.

Users and Groups_Select Login Items

The next time your selected user logs in, this app will execute. If you need the script to execute at startup, rather than login, you can simply click Login Options in this same view, and enable automatic login for your default user, like so:

Click Login Options

With this final step complete, this process will now run at login, which will now occur by default at startup, so – in effect – this process will now run at startup.

Option 2: Using launchd

launchd is Apple’s unified, open-source service management framework for starting, stopping, and managing daemons, applications, processes, and scripts. It works by reading XML files with the suffix .plist from certain directories at specified times and potentially regular intervals.

We will use launchd’s ability to execute a process at either machine startup with a launch daemon or user login with a launch agent.

In the examples below, we’ll demonstrate the structure of these files, but for an exhaustive list of possible keys to set, check out the Configuration section of https://www.launchd.info/.

2a: Creating a Launch Daemon

Launch daemons are processes that are executed by either root or the user-designated with the UserName key. They are defined by XML files with the suffix .plist that are read from a variety of locations on the system at startup, but we will put ours in the /Library/LaunchDaemons directory.




 
   Label
   com.runner.connect.plist
   RunAtLoad
   
   KeepAlive
   
   UserName
   admin
   GroupName
   staff
   InitGroups
   
   ProgramArguments
   
     /usr/bin/python3
     /Users/admin/agent/runner_connect.py
   
 

In the above, we have set a label to name our launch daemon, told the system to keep this process running, and to execute the command python3 runner_connect.py as the user “admin.” In effect, we have achieved the same outcome as we did above. This script will now run as the user admin at machine startup.

2b: Creating a Launch Agent

Launch agents are very similar to launch daemons, except for the fact that they are run as the currently logged-in user, and thus don’t execute until login. To create a launch agent, define an XML file as seen below, name it with the suffix .plist, and save it in the directory /Library/LaunchAgents.





 Label
 com.runner.connect
 ProgramArguments
 
   /usr/bin/python3
   /Users/admin/agent/runner_connect.py
 
 KeepAlive
 

TL;DR

There could be any number of use cases for executing a given process on macOS at machine startup or user login. Above, we demonstrated how to achieve both through the use of both the Automator app and launchd.