javascript - How to determine if nodejs child_process failed due to timeout? - Stack Overflow

If I do child_process.exec('mymand', { timeout: 5000 }, callback), I don't know if the r

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?

Share Improve this question asked Jan 23, 2017 at 16:15 user779159user779159 9,63216 gold badges65 silver badges94 bronze badges 2
  • 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
Add a ment  | 

1 Answer 1

Reset to default 3

There's only one way which may not be quite reliable.
So be careful when you use it for your needs

How it works:

  1. When the timeout is triggered, the parent process sends out a default SIGTERM signal unless you override in the options {killSignal : 'SIGINT'}
  2. Once the child process is exited, the parent process's call back cb(err,stdout,stderr) is called by populating err.code to null and err.signal to SIGTERM 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745093748a4610848.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信