Add a Startup Program on MacOS - Launchd

给 MacOS 添加自启动程序,让程序开机启动

Create the daemon launchd file

sudo vim /Library/LaunchDaemons/program-name.plist
<?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>program-name</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/username/go/bin/program-name</string>
        <string>-mode</string>
        <string>client</string>
        <string>-saddr</string>
        <string>conn.abc.com:8989</string>
        <!-- Add more arguments if needed -->
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>StandardErrorPath</key>
    <string>/var/log/program-name.log</string>
    <key>StandardOutPath</key>
    <string>/var/log/program-name.log</string>
</dict>
</plist>

Manage the service

By using load, the service will be started when the OS is started.

sudo launchctl load /Library/LaunchDaemons/program-name.plist
sudo launchctl unload /Library/LaunchDaemons/program-name.plist

View logs

tail -f /var/log/program-name.log