How to pass arguments to app using pm2?

后端 未结 10 1134
予麋鹿
予麋鹿 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:47

    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

    0 讨论(0)
  • 2020-12-07 17:49

    I always use PM2 to run my python scripts in Linux environment. So consider a script has a single parameter and needs to run continously after some amount if time, then we can pass it like this:

    pm2 start <filename.py> --name <nameForJob> --interpreter <InterpreterName> --restart-delay <timeinMilliseconds> -- <param1> <param2>
    

    filename.py is Name of the python script, without <> symbols, I want to run using PM2
    nameForJob is the Meaningful name for the job, without <> symbols
    InterpreterName is the python interpreter for running script, usually it is python3 in linux
    timeinMilliseconds is the time our script needs to wait and re-run again
    param1 is the first parameter for the script
    param2 is the second parameter for the script.

    0 讨论(0)
  • 2020-12-07 17:55

    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 --second-arg --third-arg
    

    Sails docs for deploymemt.

    0 讨论(0)
  • 2020-12-07 17:55

    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
    
    0 讨论(0)
  • 2020-12-07 17:55

    You can pass args for node just like that:

    NODE_TLS_REJECT_UNAUTHORIZED=0 NODE_ENV=dev pm2 start server.js --name web-server
    
    0 讨论(0)
  • 2020-12-07 17:55

    You need to start pm2 with something like: pm2 start app.js --name "app_name" -- arg1 arg2

    Then in your code, you can get your args with: console.log(process.argv);

    process.argv is a list like this: [ '/usr/local/bin/node', '/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js', 'arg1', 'arg2' ]

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