Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if nodejs child_process failed due to timeout?

If I do child_process.exec('mycommand', { 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?

like image 611
user779159 Avatar asked Sep 19 '25 02:09

user779159


1 Answers

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 command.

like image 99
manikawnth Avatar answered Sep 20 '25 17:09

manikawnth