How to pass arguments to app using pm2?

后端 未结 10 1135
予麋鹿
予麋鹿 2020-12-07 17:29

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.

相关标签:
10条回答
  • 2020-12-07 17:57

    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.

    0 讨论(0)
  • 2020-12-07 18:00

    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.

    0 讨论(0)
  • 2020-12-07 18:03

    I have tested and it works in my windows machine. Below is the complete solution to pass arguments to nodejs app using pm2.

    ** There are also 2 types of argument

    1. node-args - to use before npm start
    2. args - to use in your node program

    There are 2 ways to pass arguments with pm2.

    Option 1: pass by argument with pm2 commands.

    Option 2: by using config file e.g ecosystem.config.js

    Option 1 (Pass arg by commands):

    pm2 start app/myapp1.js --node-args="--max-http-header-size=80000" -- arg1 arg2
    
    //Access the arg as below in your node program.
    console.log(process.argv[2]); // arg1
    console.log(process.argv[3]); // arg2
    

    Option 2 (Using config file): If you are using ecosystem.config.js. you can define with the following configuration:

        {
          name: 'my-app',
          script: 'app\\myapp1.js',
          env: {
            NODE_ENV: 'DEV',
            PORT : 5051
          },
          node_args: '--max-http-header-size=80000',
          args : 'arg1 arg2',
          instances: 1,
          exec_mode: 'fork'
        }
    

    To start as dev mode:

    pm2 start --name myapp  app/myapp1.js -- .\ecosystem.config.js
    

    To start as production mode, just add --env=production

    pm2 start --name myapp  app/myapp1.js -- .\ecosystem.config.js --env=production 
    
    //Access the arg as below in your node program.
    console.log(process.argv[2]); // arg1
    console.log(process.argv[3]); // arg2
    
    0 讨论(0)
  • 2020-12-07 18:05

    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'])

    0 讨论(0)
提交回复
热议问题