Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass node v8 args and script args to pm2?

Tags:

I need to be able to start the app below with pm2 but don't know how to start it with pm2.

node --expose-gc bin/www arg1 arg2 arg3 

I know about --node-args but I think is only for --expose-gc.

like image 550
Rodrigo Pereira Avatar asked Dec 29 '14 14:12

Rodrigo Pereira


People also ask

How do I run NPM run Dev with pm2?

The following works for me. You can also specify the app name with --name argument, like this: pm2 start "npm run dev" --name myAppName Then you can see logs by pm2 logs myAppName Also, please don't use sudo until it's really necessary (in most cases, pm2 can work fine without sudo).

Why we use pm2 in node JS?

PM2 allows you to configure several different strategies for how your Node. js application should restart. By default, it restarts your application if it exits or crashes to minimize the impact to your customers in production while the source of the crash is investigated.

Where is pm2 config file?

The default configuration file is ecosystem. core3. config. js , and is located in the root folder of lisk-service : It contains a configuration to connect to a local Lisk Core node.


2 Answers

After some digging, I've found out that what I was looking for was the double dash on linux.

The normal code,

node --expose-gc bin/www arg1 arg2 arg3 

The same code using pm2

pm2 start bin/www --node-args="--expose-gc" -- arg1 arg2 arg3 

All v8 arguments you have to put inside --node-args and all scrips args to be grabbed from process.argv you have to put after the double dash.

I hope that in the future they implement something link --script-args="arg1 arg2 arg3". Would be very nice for those that isn't a linux expert.

like image 167
Rodrigo Pereira Avatar answered Oct 11 '22 18:10

Rodrigo Pereira


Another way is to create application declaration json file where you specify args key. Look at documentation on PM2 site.

Example of pm2.json file:

{   "apps" : [{     "name"        : "appname",     "script"      : "app.js",     "args"        : ["-s", "123"],     "node_args"   : "--harmony",     "merge_logs"  : true,     "cwd"         : "/this/is/a/path/to/start/script",     "env": {         "NODE_ENV": "production"     }   }] } 

And run it as follows:

$ pm2 start pm2.json 
like image 39
psulek Avatar answered Oct 11 '22 20:10

psulek