Search code examples
macosngrok

Start ngrok automatically on OSX


I am trying to start ngrok automatically when my mac mini starts up. I thought maybe OSX had something like the init-folder on ubuntu where you place scripts you want to start automatically, but i haven't found anything like it.

Are there no way to just run commands on startup on OSX?


Solution

  • You can create a LaunchDaemon to run ngrok on startup (or a LaunchAgent if you want to start ngrok after a user logs in). See the apple docs.

    A LaunchDaemon is a plist that resides in /Library/LaunchDaemons/. For example, my LaunchDaemon (/Library/LaunchDaemons/com.ngrok.onstartup.plist) looks something like this:

    <?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.ngrok.onstartup</string>
    
        <key>Program</key>
            <string>/path/to/ngrokd</string>
    
        <key>KeepAlive</key>
            <true/>
    
    </dict>
    </plist>
    

    The choice of the Label is up to you as long as it uniquely identifies your daemon. In this example my LaunchDaemon is called com.ngrok.onstartup.

    ngrokd is a bash script looking like this:

    #!/bin/sh
    /usr/local/bin/ngrok -log=/path/to/ngrok.log -config=/path/to/ngrok.yml start ssh
    

    You can check that launchd started your daemon by typing:

    sudo launchctl list com.ngrok.onstartup