3

Is possible to run node.js with timeout? In my script I am connecting to external services which sometimes don't respond. Then my script hangs and the node.js process hangs.

I would like to force exit process after 5 minutes no matter if there is an error, if the process hangs or if everything is being done correctly.

It is independent of the services I use. I just want to kill the script after 5 minutes in any situation.

2 Answers 2

2

You want to set the server.timeout property (default 0 no timeout)

Example.

var server = app.listen(<port>, function() {
  debug('Express server listening on port ' + <port>);
});
server.timeout = 300000; //5 minutes
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but I'm not using node.js as a server.
can you post snippet?
1

You can use process.exit function with exit code.

setTimeout(() => {
    process.exit(0);
}, 1000 * 60 * 5)

1 Comment

Should not have to be said, I do anyway ;) this exits the node process. regardless whats running inside. Without taking into account of performed tasks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.