Skip to content

feat: add features to stdin commands#5981

Open
qwertycxz wants to merge 5 commits into
Unitech:developmentfrom
qwertycxz:development
Open

feat: add features to stdin commands#5981
qwertycxz wants to merge 5 commits into
Unitech:developmentfrom
qwertycxz:development

Conversation

@qwertycxz
Copy link
Copy Markdown

@qwertycxz qwertycxz commented May 8, 2025

  1. Add support for input commands in pm2 logs
  2. Add support for using names in pm2 attach and pm2 send
  3. Update documentation for new features
Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets None
License Apache 2.0
Doc PR Once merged

Usage

$ pm2 attach foo # attach and send couldn't deal with name. Now they can
$ pm2 logs --attach-input # We could send message to stdin of processes in `pm2 logs`. Below is an example to send "Lorem Ipsum" to process 0.
> 0 Lorem Ipsum

qwertycxz added 4 commits May 8, 2025 13:01
1. Add support for input commands in `pm2 logs`
2. Add support for using names in `pm2 attach` and `pm2 send`
3. Update documentation for new features
@qwertycxz
Copy link
Copy Markdown
Author

I made a demo for this

Copilot AI review requested due to automatic review settings May 7, 2026 00:42
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends PM2’s stdin interaction features by allowing pm2 attach / pm2 send to target processes by name as well as id, and by adding an interactive stdin “command mode” to pm2 logs via --attach-input.

Changes:

  • Add Client.getUniqueProcessIdByName() and use it to support <id|name> in pm2 attach and pm2 send.
  • Add pm2 logs --attach-input to read stdin lines in the form <id|name> [line] ... and forward them to the targeted process stdin.
  • Update example documentation for the new logs stdin interaction flow.

Reviewed changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
types/index.d.ts Adds new type declarations for stdin/attach APIs (currently duplicates existing declarations).
lib/Client.js Introduces getUniqueProcessIdByName() helper used for name-based targeting.
lib/binaries/CLI.js Updates CLI commands to accept `<id
lib/API/Extra.js Implements name resolution in sendLineToStdin/attach and adds attachInput().
examples/interact-via-stdin/README.md Documents the new pm2 logs --attach-input workflow.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/Client.js
};

/**
* Get process id by name and check if only one process is hitted
Comment thread lib/Client.js
Comment on lines +709 to +716
* @param {string} name Name of the process to search for
* @param {(err: string, id: string) => void} cb Callback function to handle the result
*/
Client.prototype.getUniqueProcessIdByName = function(name, cb) {
this.getProcessIdByName(name, function(err, list) {
if (err) return cb(err);
if (list.length === 1) return cb(null, list[0]);
cb(`Expected exactly one process with name "${name}", but found ${list.length}.`);
Comment thread lib/binaries/CLI.js
Comment on lines 687 to 688
.action(function(pm_id, separator) {
pm2.attach(pm_id, separator);
Comment thread lib/binaries/CLI.js
Comment on lines 927 to +938
if (cmd.nostream === true)
pm2.printLogs(id, line, raw, timestamp, exclusive);
else if (cmd.json === true)
Logs.jsonStream(pm2.Client, id);
else if (cmd.format === true)
Logs.formatStream(pm2.Client, id, false, 'YYYY-MM-DD-HH:mm:ssZZ', exclusive, highlight);
else
pm2.streamLogs(id, line, raw, timestamp, exclusive, highlight);

if (cmd.attachInput) {
pm2.attachInput();
}
Comment thread lib/API/Extra.js
Comment on lines +806 to +813
rl.on('line', function(input) {
var line = input.split(' ');
that.sendLineToStdin(line.shift(), line.join(' '), function(err) {
if (err) {
that.exitCli();
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants