How to pass arguments to app using pm2?

随声附和 提交于 2019-11-27 09:44:44

问题


I am using pm2 to start my app but i am not able to pass argument to it. the command I am using is pm2 start app.js -- dev. Though this works with forever.


回答1:


You can do as stated in this ticket: https://github.com/Unitech/pm2/issues/13

Though if you're passing the environment you may want to consider leveraging environment variables. With this you create a variable which can be accessed by any process in that environment with process.env.*.

So you have a configuration file config.json:

{
   "dev": {
        "db": {
            "hosts":["localhost"],
            "database": "api"
        },
        "redis": {
            "hosts": ["localhost"]
        }
   },
   "staging": {
        "db": {
            "hosts":["1.1.1.1"],
            "database": "api"
        },
        "redis": {
            "hosts": ["2.2.2.2"]
        }
   },
   "production": {
        "db": {
            "hosts":["1.1.1.1", "1.1.1.2", "1.1.1.3"],
            "database": "api"
        },
        "redis": {
            "hosts": ["2.2.2.2", "2.2.2.3"]
        }
   }
}

Then you import your config:

var config=require('./config.json')[process.env.NODE_ENV || 'dev'];

db.connect(config.db.hosts, config.db.database);

Then you'd set the variable in your environment via shell:

export NODE_ENV=staging
pm2 start app.js

The environment variable will last as long as your session. So you'll have to set it in the ~/.bashrc file for that user for the variable to persist. This will set the variable every session.

PM2 has a deploy system which allows you to set an environment variable each time before your app is daemonized. This is how daemons in POSIX systems typically take parameters, because those parameters aren't lost with the process. Given with your circumstance it might not matter so much, but its a good practice.

Moreover you should consider stop/starting locally, and restarting(if in cluster mode) whenever possible to prevent downtime when in production.




回答2:


If you want to pass node arguments from CLI then

pm2 start myServer.js --node-args="--production --port=1337"

.

Edited

you can add any arguments after --

pm2 start app.js -- --prod

Sails docs for deploymemt.




回答3:


It is possible to define arguments with the process.

You can define a new process in ecosystem.config.js with an args key, like so:

{
  name            : 'my-service',
  script          : './src/service.js',
  args            : 'firstArg secondArg',
},
{
  name            : 'my-service-alternate',
  script          : './src/service.js',
  args            : 'altFirstArg altSecondArg',
}

Here, the two processes use the same file (service.js), but pass different arguments to it.

Note that these arguments are handled within service.js. In my case I just used process.argv[2] to get the first argument, and so on.




回答4:


You can send arguments to your script by passing them after --. For example: pm2 start app.js -i max -- -a 23 // Pass arguments after -- to app.js




回答5:


Well there are 2 ways you can do to pass the parameters from pm2 to nodejs in CLI:

  • pm2 start app.js -- dev --port=1234 (note there is an extra space between -- and dev)
  • pm2 start app.js --node-args="dev --port=1234"

Both ways, you will find these values exist in process.argv (['dev','--port=1234'])




回答6:


From the pm2 docs

//Inject what is declared in env_production
$ pm2 start app.js --env production 

//Inject what is declared in env_staging
$ pm2 restart app.js --env staging


来源:https://stackoverflow.com/questions/28980307/how-to-pass-arguments-to-app-using-pm2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!