Unitech / Unitech/pm2

PM2 scale command fails with -1 argument due to option parsing

Open
#5,932 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
43.3k
Forks
2.7k
PR merge metrics
No merged PRs in 30d

Description

What's going wrong?

When using pm2 scale <app_name> -1 to reduce the number of instances for a PM2-managed application, an error occurs because the -1 is being treated as an option, not as a valid argument to decrease the number of instances.

Root Cause

The issue arises because commander.js, which PM2 uses for argument parsing, treats -1 as an option due to the leading hyphen (-), resulting in the error. As seen in commander.js documentation, the hyphen (-) is reserved for option flags, causing unintended behavior when attempting to pass a negative number to scale down instances.

Suggested Improvement

To resolve this, it would be ideal to:

  • Support the -1 argument as a valid input for reducing the number of instances.
  • Consider using a different symbol (such as ~ or another syntax) to indicate a relative scale change, e.g., pm2 scale <app_name> ~1 to decrease by one instance.

How could we reproduce this issue?

  1. Start an application using PM2:
    pm2 start --name <app_name> app.js

  2. Attempt to scale the application down by 1 instance using:
    pm2 scale <app_name> -1

Supporting information

--- PM2 report ----------------------------------------------------------------
Date                 : Wed Dec 25 2024 10:31:22 GMT+0800 (台北標準時間)
===============================================================================
--- Daemon -------------------------------------------------
pm2d version         : 5.4.3
node version         : 22.11.0
node path            : not found
argv                 : C:\Program Files\nodejs\node.exe,C:\Program Files (x86)\nvm\v22.11.0\node_modules\pm2\lib\Daemon.js
argv0                : node
user                 : undefined
uid                  : N/A
gid                  : N/A
uptime               : 33min
===============================================================================
--- CLI ----------------------------------------------------
local pm2            : 5.4.3
node version         : 22.11.0
node path            : not found
argv                 : C:\Program Files\nodejs\node.exe,C:\Program Files\nodejs\node_modules\pm2\bin\pm2,report
argv0                : C:\Program Files\nodejs\node.exe
user                 : user
===============================================================================
--- System info --------------------------------------------
arch                 : x64
platform             : win32
type                 : Windows_NT
cpus                 : 12th Gen Intel(R) Core(TM) i7-1260P
cpus nb              : 16
freemem              : 9051037696
totalmem             : 25358680064
home                 : C:\Users\user

References
commander.js documentation (v2.15.1)
PM2 code that handles scaling
https://github.com/Unitech/pm2/blob/e3a327fab7a6b385ca0d960c878347dce320ae61/lib/API.js#L1828-L1835

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with lib/API.js at the scaling entry point referenced in the issue, then trace how the CLI parser receives the scale argument and compare that behavior with commander.js. Done means pm2 scale <app_name> -1 is accepted and reduces the instance count by one, with the existing scale behavior preserved.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.