Skip to content

Instantly share code, notes, and snippets.

@vgorloff
Last active November 17, 2022 08:58
Show Gist options
  • Save vgorloff/2e2a6acaa82075b879d954ba95f85005 to your computer and use it in GitHub Desktop.
Save vgorloff/2e2a6acaa82075b879d954ba95f85005 to your computer and use it in GitHub Desktop.
Setting up concurrently as a macOS service to server dev and prod services in local environment.

Prerequesites

  • Your NodeJS app (express, fastify, etc.) is placed at /opt/mca/www/mca-backend/app path.

Setup

  1. Install concurrently: npm install -g concurrently

  2. Create a file ~/Library/LaunchAgents/mca.backend.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>mca.backend</string>
        <key>LimitLoadToSessionType</key>
        <array>
            <string>Aqua</string>
            <string>Background</string>
            <string>LoginWindow</string>
            <string>StandardIO</string>
            <string>System</string>
        </array>
        <key>ProgramArguments</key>
        <array>
            <string>/usr/local/bin/concurrently</string>
            <string>--names</string>
            <string>dev,prd</string>
            <string>--success</string>
            <string>all</string>
            <string>--kill-others</string>
            <string>--no-color</string>
            <string>MCA_APP_STAGE=dev  node ./server.mjs</string>
            <string>MCA_APP_STAGE=prod node ./server.mjs</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>EnvironmentVariables</key>
        <dict>
            <key>PATH</key>
            <string>/usr/local/opt/node@16/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin</string>
        </dict>
        <key>WorkingDirectory</key>
        <string>/opt/mca/www/mca-backend/app</string>
        <key>StandardErrorPath</key>
        <string>/opt/mca/www/mca-backend/err.log</string>
        <key>StandardOutPath</key>
        <string>/opt/mca/www/mca-backend/out.log</string>
    </dict>
    </plist>
  3. Load and run: launchctl bootstrap gui/`id -u` $HOME/Library/LaunchAgents/mca.backend.plist

  4. Get a status: launchctl print gui/`id -u`/mca.backend

  5. Stop: launchctl kill SIGTERM gui/`id -u`/mca.backend

  6. Start/Restart: launchctl kickstart -k -p gui/`id -u`/mca.backend

  7. Unload if not needed anymore: launchctl bootout gui/`id -u`/mca.backend

IMPORTANT: Once you are loaded service with launchctl bootstrap any changes you made in file ~/Library/LaunchAgents/mca.backend.plist won't be in action until you unload the service (by using launchctl bootout) and then load it again (by using launchctl bootstrap).

Troubleshooting

See logs at: /private/var/log/com.apple.xpc.launchd/launchd.log

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment