最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论