Is there any way to forward everything in /etc/environment to PM2 processes?
Thanks!
By using process.env?
@soyuka process.env is null when a node script is started by PM2 (discovered this yesterday...).
not on my system, what's your OS? Node version? Pm2 version?
@soyuka ubuntu 14.04.1, node 0.10.33, pmv 0.12.1.
Load env-vars is not PM2's job, it seems that node does not load variables from /etc/environment.
@klinquist, you must known that the /etc/environment only takes effect on login, that means when you update the /etc/environment file to add a new env-var, you need to logout then login in order to make it taking effect.
You can take a test:
$ echo "GOOD=BOYS" >> /etc/environment
$ env | grep "GOOD" | wc -l
# OUTPUT: 0
$ exit
# TODO: login
$ env | grep "GOOD" | wc -l
# OUTPUT: 1
@Tjatse I have an ansible script that sets the vars in /etc/environment _then reboots the machine_, so that isn't the issue.
@klinquist You mean the ansible tool? Could you just run $ env and make sure your variables do exist.
@Tjatse Yes, they exist. console.log(process.env) works when calling the node script directly. When I start the script through PM2 then do 'pm2 logs' I can see process.env = undefined.
Nice, that's clear enough and help a lot, I'll setup a new Ubuntu VM to take a complete test.
+1
I can not reproduce this, any other hint?
ENV: ubuntu 14.04.1 LTS server, node v0.10.33, [email protected].
test.js:
console.log(process.env);
setTimeout(function(){}, 300000);

I think the environment variables do load when started this way, but NOT, when started with pm2 deploy from another system... Not sure if this is what you guys are concerned, but I am actually trying to get all my env-vars included as well...
I may be wrong, will test...
Just tested, and can confirm:
When running your test.js script with pm2 start test.js --> I get all my environment variables included.
BUT when doing pm2 deploy ... with testing in the post-deploy hook I get wayyy less environment variables included /:
Tested on:
@Unitech @jshkurti @soyuka plz ignore my previous comment.
I've test it again, it seems okay.
DEPLOY

MY TEST ENV

RESULTS

TEST CASE
https://github.com/Tjatse/pm2-deploy-test
with testing in the post-deploy hook I get wayyy less environment variables included /:
@glesage Can you provide a further information, thanks.
@Tjatse yea sure, so I just tested it again, and it does indeed NOT include my general environment variables when done with pm2 deploy...
Note the MY_VAR_1 & MY_VAR_2 vars
SSH into my server and do node test.js:
{
"XDG_SESSION_ID": "1",
"COMP_WORDBREAKS": " \t\n\"'><;|&(:",
"TERM": "xterm-256color",
"SHELL": "/bin/bash",
"SSH_CLIENT": "216.80.23.100 55243 22",
"SSH_TTY": "/dev/pts/0",
"USER": "root",
"MAIL": "/var/mail/root",
"PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games",
"MY_VAR_1": "TEST 1",
"PWD": "/root/web/public",
"MY_VAR_2": "TEST 2",
"LANG": "en_US.UTF-8",
"SHLVL": "1",
"HOME": "/root",
"LOGNAME": "root",
"SSH_CONNECTION": "216.80.23.100 55243 104.236.110.107 22",
"LESSOPEN": "| /usr/bin/lesspipe %s",
"XDG_RUNTIME_DIR": "/run/user/0",
"LESSCLOSE": "/usr/bin/lesspipe %s %s",
"_": "/usr/bin/node",
"OLDPWD": "/root"
}
SSH into my server and do pm2 start test.js:
{
"name": "test",
"exec_mode": "fork_mode",
"exec_interpreter": "node",
"pm_exec_path": "/root/web/public/test.js",
"env": "[object Object]",
"pm_cwd": "/root/web/public",
"instances": "1",
"node_args": "",
"pm_out_log_path": "/root/.pm2/logs/test-out-0.log",
"pm_err_log_path": "/root/.pm2/logs/test-error-0.log",
"pm_pid_path": "/root/.pm2/pids/test-0.pid",
"XDG_SESSION_ID": "1",
"COMP_WORDBREAKS": " \t\n\"'><;|&(:",
"TERM": "xterm-256color",
"SHELL": "/bin/bash",
"SSH_CLIENT": "216.80.23.100 55243 22",
"SSH_TTY": "/dev/pts/0",
"USER": "root",
"MAIL": "/var/mail/root",
"PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games",
"MY_VAR_1": "TEST 1",
"PWD": "/root/web/public",
"MY_VAR_2": "TEST 2",
"LANG": "en_US.UTF-8",
"SHLVL": "1",
"HOME": "/root",
"LOGNAME": "root",
"SSH_CONNECTION": "216.80.23.100 55243 104.236.110.107 22",
"LESSOPEN": "| /usr/bin/lesspipe %s",
"XDG_RUNTIME_DIR": "/run/user/0",
"LESSCLOSE": "/usr/bin/lesspipe %s %s",
"_": "/usr/local/bin/pm2",
"OLDPWD": "/root",
"status": "launching",
"pm_uptime": "1419169833285",
"axm_actions": "",
"axm_monitor": "[object Object]",
"axm_options": "[object Object]",
"axm_dynamic": "[object Object]",
"created_at": "1419169833285",
"pm_id": "0",
"restart_time": "0",
"unstable_restarts": "0",
"started_inside": "false",
"command": "[object Object]"
}
Doing pm2 deploy commands (setup & then deploy):
{
"SHARED": "/root/web/shared",
"XDG_SESSION_ID": "9",
"SHELL": "/bin/bash",
"SSH_CLIENT": "216.80.23.100 55407 22",
"OLDPWD": "/root",
"USER": "root",
"MAIL": "/var/mail/root",
"PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games",
"PWD": "/root/web/current",
"LANG": "en_US.UTF-8",
"SHLVL": "1",
"HOME": "/root",
"LOGNAME": "root",
"SSH_CONNECTION": "216.80.23.100 55407 104.236.110.107 22",
"XDG_RUNTIME_DIR": "/run/user/0",
"_": "/usr/bin/node"
}
@glesage can you provide the sample codes of ecosystem.json (which you deployed).
Yea so in this case I simplified it (replaced npm install to node test.js in the post-deploy section) just so it'd be a little quicker.. but the result is the same:
{
apps : [
{
name : "My site",
script : "node.js",
env_production : {
NODE_ENV: "production"
}
}
],
deploy : {
dev : {
user : "root",
host : "my_ip",
ref : "origin/dev",
repo : "my_repo",
path : "/root/web",
"post-deploy" : "node test.js && pm2 startOrRestart ecosystem.json --env dev",
env : {
NODE_ENV: "dev"
}
}
}
}
Appreciate, @glesage.
pm2-deploy is using ssh to deploy git repo, after looked into /tmp/pm2-deploy.log I found this:
ssh [user]@[ipv4] "cd /var/www/current; SHARED="/var/www/shared" node test.js && pm2 startOrRestart ecosystem.json --env dev 2>&1 | tee -a /tmp/pm2-deploy.log; exit ${PIPESTATUS[0]}"
The problem is that ssh is a non-interactive shell, /etc/profile (could be any other) will never be resolved, unless we do source /etc/profile before doing the deployment:
ssh [user]@[ipv4] "source /etc/profile; cd /var/www/current; SHARED="/var/www/shared" node test.js && pm2 startOrRestart ecosystem.json --env dev 2>&1 | tee -a /tmp/pm2-deploy.log; exit ${PIPESTATUS[0]}"
We can solve this in a DRY way.
"post-deploy" : "source /etc/profile && node test.js && pm2 startOrRestart ecosystem.json --env dev"
If your vars are stored in /etc/environment, make sure your ssh user has root permission.
head over to Stackoverflow get more information.
Other solutions from stackoverflow:
1. Add vars in ~/.ssh/environment
Shell environment does not load when running remote ssh command. You can edit ssh environment file:
vi ~/.ssh/environment
Its format is:VAR1=VALUE1
VAR2=VALUE2
Also, check sshd configuration for PermitUserEnvironment=yes option.
2. Edit ~/.bashrc
I had similar issue, but in the end I found out that ~/.bashrc was all I needed.
However, in Ubuntu, I had to comment the line that stops processing ~/.bashrc :If not running interactively, don't do anything
[ -z "$PS1" ] && return
@Unitech Maybe you need to update the pm2-deploy module to enforce source /etc/profile before deploying (I'm not familiar with bash x.-) or simply update the ADV_RM to notify people.
oh indeed, alright I will take a look... thanks!
adding source /(...)/my_vars_file in post-deploy works great for me, thanks alot!
Is the bug fixed yet? I've tried all ideas above, I think, but it still doesn't work.
I logged a comprehensive question here: http://stackoverflow.com/questions/34897875/how-do-i-set-linux-environment-variables-so-that-pm2-can-see-them-for-node-js-ap.
I just ran into this issue on ubuntu 14.04 , node 6.9.1. My issue arose when a running server went down and a new one was brought back up without my application code deployed on it (but with a couple environment variables exported into /etc/environment). So I deployed my code to the new server (at the end of which a script calls pm2 start) but with only the aforementioned environment variables in place. So I manually entered the necessary additional environment variables in /etc/environment, logged out of the server and back in, then ran pm2 restart all, and all the newly added environment variables were showing up undefined when I logged them to the console.
~/.bash_profile and running source ~/.bash_profile, but that didn't work.DEFAULT env_keep="..." in /etc/sudoers, logged out and back in, but that didn't work. pm2 stop all, noted the id of the process for the application (0), deleted the application process with pm2 delete 0, then ran pm2 start server.js... from scratch and the variables were logged correctly.Anyway, I hope this helps someone else in the future.
New option has been added to update environnements just use --update-env via CLI or updateEnv: true in process file (for pm2 2.1^)
Oof, wish I had known that a few hours ago, haha. Thanks for the heads up @vmarchaud!
Most helpful comment
New option has been added to update environnements just use
--update-envvia CLI orupdateEnv: truein process file (for pm22.1^)