If I do child_process.exec('mymand', { timeout: 5000 }, callback)
, I don't know if the resulting error is caused by a timeout or some other reason. Is there a way to determine whether the failure was caused by the { timeout: 5000 }
option passed to child_process
?
If I do child_process.exec('mymand', { timeout: 5000 }, callback)
, I don't know if the resulting error is caused by a timeout or some other reason. Is there a way to determine whether the failure was caused by the { timeout: 5000 }
option passed to child_process
?
- The first parameter received by the callback contains an error object. Is not this what you need? exec('mand', {...}, function (error) { if (error) { console.error(error); }) – Gabriel Oliveira Commented Jan 23, 2017 at 16:25
- The error object contains the stderr of the mand, but no information that hints at whether it was killed due to timeout. – user779159 Commented Jan 23, 2017 at 19:54
1 Answer
Reset to default 3There's only one way which may not be quite reliable.
So be careful when you use it for your needs
How it works:
- When the timeout is triggered, the parent process sends out a default
SIGTERM
signal unless you override in the options {killSignal : 'SIGINT'} - Once the child process is exited, the parent process's call back
cb(err,stdout,stderr)
is called by populatingerr.code
tonull
anderr.signal
toSIGTERM
or whatever it is
So you can check for err.signal
in the callback. And again this is not quite reliable as you can kill the child process from task manager or using shell's kill mand.