Skip to content

Can't supply command line arguments to script #13

Closed

Description

@Morriz

Try:

pm2 start 'script.js -d 1'

and it crashes, showing a module not found error:

Error: Cannot find module '......./device-simulation.js -d 1'

Activity

  1. Unitech commented on Jun 19, 2013

    @Unitech
    Owner

    Not sure we can do that easily for now, as require is used for launching the script.
    The alternative would be to use environment variables instead.

    CLI Code :
    https://github.com/Unitech/pm2/blob/master/bin/pm2#L235

    Fork child ProcessContainer.js :
    http://nodejs.org/api/cluster.html#cluster_cluster_fork_env

    ProcessContainer script require :
    https://github.com/Unitech/pm2/blob/master/lib/ProcessContainer.js#L56

  2. Morriz commented on Jun 19, 2013

    @Morriz
    Author

    I think it would be really easy to parse extra params within single quote chars and enrich process.argv with them.

    On 19 jun. 2013, at 05:49, Alexandre Strzelewicz notifications@github.com wrote:

    Not sure we can do that easily for now, as require is used for launching the script.
    The alternative would be to use environment variables instead.

    CLI Code :
    https://github.com/Unitech/pm2/blob/master/bin/pm2#L235

    Fork child ProcessContainer.js :
    http://nodejs.org/api/cluster.html#cluster_cluster_fork_env

    ProcessContainer script require :
    https://github.com/Unitech/pm2/blob/master/lib/ProcessContainer.js#L56

    —
    Reply to this email directly or view it on GitHub.

  3. Unitech commented on Jun 19, 2013

    @Unitech
    Owner

    That's a solution, pull me a request 👍 ! :)

  4. Morriz commented on Jun 19, 2013

    @Morriz
    Author

    Not behind comp today nor have time soon, but will do at some point...

    On 19 jun. 2013, at 12:54, Alexandre Strzelewicz notifications@github.com wrote:

    That's a solution, pull me a request ! :)

    —
    Reply to this email directly or view it on GitHub.

  5. rlidwka commented on Jun 19, 2013

    @rlidwka
    Contributor

    A standard unix way of doing such things would be something like that:
    pm2 start script.js -- -d 1

    when anything after "--" parameter is passed to the script

    (see http://unix.stackexchange.com/questions/11376/what-does-double-dash-mean)

    PS: I suppose, commander can already do that, just add something like process.argv = commander.argv after a require.

  6. Morriz commented on Jun 19, 2013

    @Morriz
    Author

    Nice. Learned a thing on my day off ;)

    On 19 jun. 2013, at 18:43, Alex Kocharin notifications@github.com wrote:

    A standard unix way of doing such things would be something like that:
    pm2 start script.js -- -d 1

    when anything after "--" parameter is passed to the script

    (see http://unix.stackexchange.com/questions/11376/what-does-double-dash-mean)

    —
    Reply to this email directly or view it on GitHub.

  7. added a commit that references this issue on Jun 23, 2013
  8. Unitech commented on Jun 23, 2013

    @Unitech
    Owner

    Fixed and thanks for the tip Alex ;)

  9. xieranmaya commented on Feb 23, 2016

    @xieranmaya

    NOTICE that the arguments you supply will persist until you delete and restart the app:

    pm2 start app.js -- aa bb cc
    pm2 restart app.js -- 11 22 33

    Above will not work, you should do the following:

    pm2 delete app.js
    pm2 start app.js -- 11 22 33

    However I think it's weird, what if I want to change an argument...

  10. govo commented on May 19, 2016

    @govo

    @xieranmaya Thank you!

  11. Unitech commented on May 19, 2016

    @Unitech
    Owner

    Now with PM2 > 1.1.x

    this:

    $ pm2 start app.js -- aa bb cc
    $ pm2 restart app.js -- 11 22 33
    

    works, but doing

    $ pm2 restart app.js

    Will still keep the latest arguments (11, 22, 33)

  12. xieranmaya commented on May 21, 2016

    @xieranmaya

    @Unitech Great improve!

  13. martinjuhasz commented on Jun 23, 2016

    @martinjuhasz

    Is there a way to run two processes with different arguments but the same script with this configuration?
    It also seems that i cant put more than 8 arguments in there:

    pm2 start log --name "Logger" --node-args='--harmony_destructuring' -- arg1 arg2 arg3 arg4 arg5 arg6 arg7 arg8
    [PM2] Starting log in fork_mode (1 instance)
    [PM2] Done.
    
    pm2 start log --name "Logger" --node-args='--harmony_destructuring' -- arg1 arg2 arg3 arg4 arg5 arg6 arg7 arg8 arg9 
    [PM2][ERROR] script not found : /somedir/bin/arg9
    
  14. SonicReal commented on Oct 20, 2016

    @SonicReal

    Now with PM2 > 1.1.x

    this:

    $ pm2 start app.js -- aa bb cc
    $ pm2 restart app.js -- 11 22 33
    works, but doing

    $ pm2 restart app.js
    Will still keep the latest arguments (11, 22, 33)

    Hello. I dont need to keep latest arguments. How can i "nullify" them?

  15. anoop-ananthan commented on Sep 2, 2019

    @anoop-ananthan

    What is the way to run
    pm2 start --env production --node-args "DB_HOST=some.host.xyz DB_NAME=mybase DB_USER=heman DB_PASSWORD=Morning@r00t90"

  16. michaeldun123 commented on Aug 27, 2025

    @michaeldun123

    trying to run with python a variable for a config file, and I keep getting issues. Any assistance or best to open a new issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions