2

I've been browsing around but to no success. I've found some npm packages like nodemon and forever but documentation doesn't explain how to call a restart inside the script properly.

I've found this code snippet on another question but I'm not using Express or other frameworks since the script is using a pulling service not a response one.

This is code I've made so far using internal Node.js dependencies but no luck.


'use strict'

process.on('uncaughtException', (error) => {
  console.error('Global uncaughtException error caught')
  console.error(error.message)
  console.log('Killing server with restart...')
  process.exit(0)
})

process.on('exit', () => {
  console.log('on exit detected')
  const exec = require('child_process').exec
  var command = 'node app.js'
  exec(command, (error, stdout, stderr) => {
    console.log(`error:  ${error.message}`)
    console.log(`stdout: ${stdout}`)
    console.log(`stderr: ${stderr}`)
  })
})

setTimeout(() => {
  errorTriggerTimeBomb() // Dummy error for testing triggering uncaughtException
}, 3000)

Just to note I'm running the server on Termux, a Linux terminal app for android. I know it's better to run from desktop but I'm always at a WiFi or mobile data area and that I don't like leaving my PC on overnight.

Nova
  • 2,000
  • 3
  • 14
  • 24

1 Answers1

4

A typical restart using something like nodemon or forever would be triggered by calling process.exit() in your script.

This would exit the current script and then the monitoring agent would see that it exited and would restart it for you. It's the same principal as if it crashed on its own, but if you're trying to orchestrate it shutting down, you just exit the process and then the monitoring agent will restart it.

I have a home automation server that is being monitored using forever. If it crashes forever will automatically restart it. I also have it set so that at 4am every morning, it will call process.exit() and then forever will automatically restart it. I do this to prevent any memory leak accumulation over a long period of time and 30 seconds of down time in the middle of the night for my application is no big deal.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Strange, I tried nodemon but I couldn't get `process.exit()` to cause nodemon to restart it. Do you have a minimum code snippet I could try with comments separating between nodemon script and the app.js script? – Nova Feb 02 '18 at 04:54
  • @Nova - I believe nodemon is more about restarting your script when any of your script files are modified (useful for development). It will restart your script when it exits too, but I don't think that's really the main thing it was built for. That's why I use [forever](https://www.npmjs.com/package/forever). I just start my app with `forever start server.js`. – jfriend00 Feb 02 '18 at 05:04
  • So process.exit() should cause forever to restart app.js? – Nova Feb 02 '18 at 05:09
  • @Nova - Yes, if you used `forever` to start your app, the `process.exit()` will exit your app and `forever` will then restart it. As I said in my answer, I use that exact thing for an automatic 4am restart in a home automation server. – jfriend00 Feb 02 '18 at 05:12