When spawning a child process with `require('child_process').spawn
, a shell spawn for few milliseconds (almost one seconds) on Windows.
pm2's users are reporting us this issue (here or here) , which come from this code that we are using to spawn the user app.
Just asking why it is spawning and if we can prevent them to spawn ?
Thanks !
See https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_child_process_spawn_command_args_options -- make sure the shell
option is false
.
Let me know if it is false
but still spawning a shell. I'm unsure but it could be necessary. Or, it may be how spawn()
has to work on windows, exec()
may not have the issue. Not sure.
As you can see here, and as the documentation tell Defaults to false (no shell)
, we spawn with the shell set to false.
So no, its still spawning a shell, i will try asap with exec
(as i remember its spawning a shell too, but not sure, i will re-test)
When spawning a child process with `require('child_process').spawn, a shell spawn for few milliseconds (almost one seconds) on Windows.
When you say 'spawns a shell', do you mean it opens a console window?
@bnoordhuis Yes
Right. That's a property of the executable you are spawning and not something node.js has direct control over. Closing, not a bug.
You aren't running npm with detached
. That could be why.
It's a flag in the PE header of the .exe that controls whether a console window is opened or not. IOW, whether an application is a console application or not is something that is decided at compile time.
You can suppress it up to a point by passing { detached: true }
to spawn() but there is nothing stopping the child process from creating a console window manually or spawning children that create one.
We are using childProcess.fork().
When we run "node myapp.js" from the command line, everything is fine.
When we run "pm2 start myapp.js", cmd windows pop up everywhere.
I find it hard to believe people aren't having major problems with this. The pm2 behavior does not mimic the node behavior. Trying to push a production app onto a windows device is no longer possible with pm2 as continual popups are not an option.
Whether it is a bug or not is inconsequential. The point is: there has to be a way around this problem.
Thanks
@wayofthefuture did you ever find a solution to your issue? I seem to have the same problem. Straight running the app using "node app.js" works but running the app in pm2 spawns consoles.
Yup! Here's the solution:
https://github.com/kohsuke/winsw
I ran into problems with pm2 in both linux and windows. So now I use systemd and winsw and no longer use pm2, just straight up node myapp.js using a service... not to mention the permission problems and startup problems involved with cross-platform implementation of pm2.
Looks like someone has opened a new issue (https://github.com/nodejs/node/issues/15217) and a solution has been found, just need to implement it
Most helpful comment
We are using childProcess.fork().
When we run "node myapp.js" from the command line, everything is fine.
When we run "pm2 start myapp.js", cmd windows pop up everywhere.
I find it hard to believe people aren't having major problems with this. The pm2 behavior does not mimic the node behavior. Trying to push a production app onto a windows device is no longer possible with pm2 as continual popups are not an option.
Whether it is a bug or not is inconsequential. The point is: there has to be a way around this problem.
Thanks