Systemctl Dynamic Path: AWS EC2 + AWS Linux2
-
Hi All,
The recommended setup for nvm/npm/nodejs is per user.
Lets assume you created a user nodebb, installed nodejs under it, and revised .bashrc to set environment variables to point to the latest version (standard procedure):
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion" #Systemctl expects full path to executable. Unfortunately, that path may change when nodejs gets updated.
So how do we hack systemctl to use a dynamic path?
- Generate the env variables and dump them into a file
- Load the file into systemctl environment
See config below:
[Unit]
Description=NodeBB Service
After=system.slice multi-user.target mongod.service syslog.target[Service]
Type=idle
User=nodebbStandardOutput=syslog
StandardError=syslog
SyslogIdentifier=nodebbExecStartPre=/bin/bash -c -l '/usr/bin/env > /tmp/tmp_env ; /bin/echo NODE_ENV=production >> /tmp/tmp_env'
EnvironmentFile=/tmp/tmp_env
WorkingDirectory=/path_to_nodebbExecStart=/usr/bin/env node loader.js --no-silent --no-daemon
Restart=always[Install]
WantedBy=multi-user.targetPay attention: Type=idle replaces Type=simple.
Good luck!
JJ.