-
Notifications
You must be signed in to change notification settings - Fork 291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Detecting if a child process spawned successfully #1191
Comments
One thing you could do, is in the child process, write to stdout "spawned successfully", read from stdout in the parent process, and then wait for that message. const k = cp.spawn('bash');
let stdout = '';
k.on('data', function(d){
stdout+= String(d);
if(stdout.match(/spawned successfully/)){
// do your thing
}
}); |
I’m pretty sure there was some very similar issue here a while ago, but I can’t seem to find it … anyway:
Sadly, no, not in the general case.
Yup, that’s correct. libuv does a best-effort thing here, but it’s impossible to get a guaranteed result without resorting to e.g. polling techniques that look up the process pid and what that process is currently doing.
Part of the problem here is that there is no clear line for determining what a “failed” spawn attempt means – at what point do you consider a process spawned successfully? There can be errors at any point during process setup.
On Unix, that the On Windows, a similar thing goes for Either way, there are still plenty of reasons why starting a process might fail – missing DLLs/shared libraries, memory exhaustion while running setup code, etc.
If the
For checking whether the executable could be found, yes, that’s reliable.
The only thing that comes to my mind would be polling the OS’s way of providing information about the process tree in general.
When, and most importantly, from where would that callback be called?
(Side note: Node 7 has been unmaintained for ~ 9 months, you might want to consider upgrading to an actively maintained version. Here’s the schedule. :)) |
@pushkin- no problem |
Thank you so much @ORESoftware - simple and handy |
child_process
Is there anyway to detect if a process has successfully spawned? There's an
error
event that I can listen to, but nosuccess
event, so it seems that I have no guarantee that the process successfully spawned until I start actually writing to its streams and get errors.I have found that I can check
proc.pid
right afterspawn
ing, and if I pass in a faulty path, it'sundefined.
If I pass in the correct path, it's set to a number, which seems promising. However, someone on SO said that the spawning might still fail after the OS gives back a pid.What exactly is guaranteed after the
spawn
function returns? At what point in the spawning process are we? And can I rely on thisproc.pid === undefined
technique? If not, how can I at least asynchronously know if the process successfully spawned? Why is there nosuccess
callback?The text was updated successfully, but these errors were encountered: