pm2 icon indicating copy to clipboard operation
pm2 copied to clipboard

pm2运行js文件,如果js文件有和终端交互的 该怎么运行呀(比如输入1、2)

Open hardwin1014 opened this issue 1 year ago • 7 comments

What's going wrong?

How could we reproduce this issue?

Supporting information

# Run the following commands
$ pm2 report

hardwin1014 avatar Jul 06 '24 04:07 hardwin1014

?

dimitriaatos avatar Jul 08 '24 09:07 dimitriaatos

Cant do like this

hengistchan avatar Jul 17 '24 04:07 hengistchan

I think you're looking for pm2 attach <pm_id> The only docs I was able to find about this command is by running pm2 --help which logs among other

attach <pm_id> [comman]      attach stdin/stdout to application identified by <pm_id>

Here's an example using the command

//greetings.js
const readline = require('node:readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.question('What\'s your name? ', name => {
  console.log(`Hi ${name}!`);
  rl.close();
});

You can interact with greetings.js by running the following

pm2 start greetings.js #lets say greetings gets assigned to id 0 you can verify that by running
pm2 id greetings #returns [ 0 ]
pm2 attach 0
Dimitri #returns "Hi Dimitri!"

dimitriaatos avatar Jul 19 '24 19:07 dimitriaatos

When I was using python scripts written by others, I found that python and terminal interaction could be abbreviated as such python3 main.py --action (1/2) Writing in this way, you can pass the parameters you want to fill to the terminal input, js has no such writing

You can probably make your node scripts behave like that by using arguments but I don't think it's native behaviour. This is irrelevant to pm2 though.

dimitriaatos avatar Aug 10 '24 13:08 dimitriaatos