I am running a node webserver using pm2. Since pm2 spawns another process and redirects stdout and stderr to files, I have to look somewhere else for the logs. Ideally, I would like to have the node process output to the same console window that I've run pm2 from. Otherwise, I would settle for pm2 run the node process with an active console window and have stdout and stderr of the node process write to that console window. How can this be achieved? I'm on a windows machine.
7 Answers
I believe you can also see the stdout and stderr of a process that is running daemonized by the command pm2 logs
or pm2 logs [app-name]
.

- 2,537
- 2
- 20
- 23
-
2accepting this answer since it addresses the first log being missed with th `--no-daemon` flag – Fragilerus Mar 30 '16 at 22:17
-
what do you mean by "deamonized"? – João Pimentel Ferreira Feb 19 '19 at 20:38
you can easily achieve that by starting another terminal/console and run this command
pm2 log
logs everything to the terminal except console.log
pm2 logs
logs everything to the terminal and console.log
notice the 's' in the second command

- 6,687
- 5
- 44
- 67
Found the answer (their documentation is not that great), just added the --no-daemon
flag, seems to have done it. Although, it appears that it's still logging to the file (even when using the flag) on the first uptime. Once the process gets restarted (I'm watching for file changes) it starts logging out to the console

- 1,829
- 3
- 15
- 22
programmatically you can do something like this:
const pm2 = require('pm2')
pm2.connect(function(err) {
if (err) {
console.error(err);
process.exit(2);
}
pm2.start([
{
script : "server.js",
output: "/dev/stdout",
error: "/dev/stderr",
},
]
, function(err, proc) {
if(err) {
throw err
}
});
})

- 12,020
- 17
- 63
- 121
-
1You can also set output and error options to be `/dev/stdout` and `/dev/sterr` respectively. – mauricio Nov 12 '19 at 23:58
-
6It would be good to stdOut to console!! Do anyone of you know how to do it ? – TOPKAT Jan 31 '20 at 17:39
Direct to the app running that you want to monit logs
pm2 logs <app id or app name> --lines 50

- 228
- 3
- 13
I think --attach
flag might be the best option here.
From docs:
To start an app and check logs stream use the --attach option:
$ pm2 start api.js --attach
When quitting via Ctrl-C, the app will still run in background.

- 5,903
- 1
- 34
- 38
I wanted to run pm2 in background of an nginx-based docker container. Added a shell script into /docker-entrypoint.d/
which just runs ./node_modules/.bin/pm2-runtime start ./ecosystem.config.cjs &
. The & makes pm2 run in background, but all the stdout is unaffected - it all gets mixed in with normal nginx stdout.

- 23,026
- 8
- 58
- 72