- Your NodeJS app (express, fastify, etc.) is placed at
/opt/mca/www/mca-backend/app
path.
-
Install concurrently:
npm install -g concurrently
-
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>
-
Load and run:
launchctl bootstrap gui/`id -u` $HOME/Library/LaunchAgents/mca.backend.plist
-
Get a status:
launchctl print gui/`id -u`/mca.backend
-
Stop:
launchctl kill SIGTERM gui/`id -u`/mca.backend
-
Start/Restart:
launchctl kickstart -k -p gui/`id -u`/mca.backend
-
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
).
See logs at: /private/var/log/com.apple.xpc.launchd/launchd.log