如何设置Linux环境变量,以便pm2可以看到node.js应用程序的变量?

时间:2016-01-20 10:47:03

标签: linux node.js ubuntu pm2

当我使用node server.js在我的Ubuntu Linux服务器上运行我的nodejs应用程序时,它可以正常工作,并使用 process.env.db 输出环境变量$db的值

但是,在运行sudo pm2 start server.js时,应用会中断,将环境变量视为未定义

我尝试在以下文件中添加变量:

  • etc / environment:db="hello"
  • 〜/ .ssh / environment:db="hello"
  • 〜/ .bashrc:export db="hello"

我也重新启动并运行source ~/.bashrc以确保变量可用。

我想我已经尝试了这里提到的所有内容,我不知道还能做什么:

2 个答案:

答案 0 :(得分:7)

请注意,您正在加载当前用户的变量source ~/.bashrc。但是,如果您说sudo ...与用户root一起运行,那么这不会改变。

您可以做的是sudo-E

一起使用
sudo -E pm2 start server.js

来自man sudo

-E, --preserve-env
             Indicates to the security policy that the user wishes to reserve their
             existing environment variables.  The security policy may eturn an error
             if the user does not have permission to preserve the environment.

答案 1 :(得分:1)

请参阅此主题https://github.com/Unitech/pm2/issues/204

好像你的环境变量被缓存了。

相关问题