Topic 1 Posts

autostart

Autostart scripts and services on Mac OS with launchd

When it comes to autostarting custom scripts/services after booting your Mac, adding stuff to start with your system on Mac OS might now be always possible via System Preferences.app -> Users & Groups -> Login Items. [1]

So in order to make your script or service to launch on start, follow my example on running a DynDNS service on boot and each 5 minutes afterwards.

First start by creating a shell script that launches your service. In my case that's a script that curl's a specific url with parameters of my current IP to assign it to the domain I'm using:
nano ~/Documents/dyndns.sh

with:

IP=$(curl ifconfig.co)
curl "https://dynamicdns.park-your-domain.com/update?host=@&domain=example.com&$

Next I create a system service plist:
nano /Library/LaunchDaemons/dyndns.plist

And enter the following:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
        <dict>
                <key>Label</key>
                <string>com.igor.dyndns</string>
                <key>ProgramArguments</key>
                <array>
                <string>/bin/sh</string>
                <string>/Users/igor/Documents/dyndns.sh</string>
                </array>
                <key>RunAtLoad</key>
                <true/>
                <key>StartInterval</key>
                <integer>300</integer>
        </dict>
</plist>

You can avoid the StartInterval key if what you need is just to load on start. You also might label your service something else than com.igor.dyndns

If you're launching your service via shell or you just have a shell script, you'll have to add the absolute path to it via <string>/bin/sh</string> followed by the absolute path to your script within <key>ProgramArguments</key>. If you're not using shell, you just have to specify what you're launching using absolute paths.

My personal recommendation is to specify one launch script here and enter eveything what needs to be launched to that script and not to the plist itself.

Last step is to load your custom service to launchd via:
sudo launchctl load -w /Library/LaunchDaemons/dyndns.plist


  1. Since we're here, you can visit these settings in order to remove something unwanted from your startup which might got there without your consent and might actually slow down your system startup ↩︎