简体   繁体   English

PM2环境变量缓存

[英]PM2 environment variables caching

I am running PM2 on Ubuntu 16.04 and it seems that environment variables are being cached somehow. 我在Ubuntu 16.04上运行PM2,似乎环境变量以某种方式被缓存。 Is there an way of seeing which environment variables PM2 are using. 有没有办法看到PM2正在使用哪些环境变量。 The environment variables it can somehow see are not available in my terminal session anymore echo $VAR_NAME . 它可以以某种方式看到的环境变量在我的终端会话中不再可用echo $VAR_NAME

I created the environment variables like this: 我创建了这样的环境变量:

export VAR_NAME=value

Removing the environment variable using: 使用以下方法删除环境变量

unset VAR_NAME

doesn't work PM2 is stubbornly holding on to the environment variable - even after various restart & ssh sessions. 不起作用PM2顽固地坚持环境变量 - 即使在各种重启和ssh会话之后。 Render me confused :-/ 让我困惑: - /

Is there a way of flushing the environment variables PM2 is using? 有没有办法清除PM2正在使用的环境变量? Or at least seeing which environment variables it knows about? 或者至少看看它知道哪些环境变量?

Update to the original answer: 更新到原始答案:

If the environment variables' values are preset, as in the case of having different env variables for development, staging, and production, there is an option using a process.json file. 如果预设了环境变量的值,就像具有用于开发,登台和生产的不同env变量的情况一样,则有一个使用process.json文件的选项。

The below is a sample for a node.js app: 以下是node.js应用程序的示例:

{
  "apps" : [{
    "env": { 
     // in this section you would list variables that you 
     // want available in all cases
      "NODE_PATH": "..."
    },
    "env_development": {
      "CONFIG": "dev.conf.json",
      "NODE_ENV": "development"
    },
    "env_production" : {
       "CONFIG": "conf.json",
       "NODE_ENV": "production"
    },
    "exec_mode": "fork", // or cluster if that's what you want
    "name"        : "my_app",
    "script"      : "/var/my_app/app.js", //srcipt's path
    "watch"       : false // don't restart on file changes
  }]
}

Having this file defined, with the possible values for env, you can switch environment by restarting the app as follows: 定义此文件,并使用env的可能值,您可以通过重新启动应用程序来切换环境,如下所示:

  1. Start the app normally: pm2 start process.json --env development 正常启动应用程序: pm2 start process.json --env development

  2. When you want to switch to a different env: pm2 restart process.json --env production 当你想切换到另一个env时: pm2 restart process.json --env production

For more about process.json and the possible options: PM2 - Process File 有关process.json和可能的选项的更多信息: PM2 - Process File


Original answer: 原始答案:

You have to kill pm2 first. 你必须先杀掉pm2。

pm2 kill

pm2 start app.js

PM2 preserves the environment variables it read upon starting, it does not reread their values every time. PM2保留了它在启动时读取的环境变量,它不会每次重读它们的值。

I searched for it quickly, and found this issue on github: https://github.com/Unitech/pm2/issues/83 , and Unitech's answers confirm this. 我快速搜索了它,并在github上找到了这个问题: https//github.com/Unitech/pm2/issues/83,Unitech的回答证实了这一点。

In this particular comment: https://github.com/Unitech/pm2/issues/83#issuecomment-29837221 在这个特别评论: https//github.com/Unitech/pm2/issues/83#issuecomment-29837221

Unitech says: Unitech说:

Yep this is normal in "cluster_mode". 是的,这在“cluster_mode”中是正常的。 As pm2 wrap your code to his own context (and own variables) you get what was already there when launching pm2. 当pm2将你的代码包装到他自己的上下文(和自己的变量)时,你会得到启动pm2时已经存在的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM