Restart a node process from within itself

Is it possible to restart a running single-threaded node process entirely from within itself?

I imagine the steps to take for a server would be something like this:

  • Close the listening sockets
  • Fork off a child through either child_process.forkor child_process.spawn, handing over process.argv to the child.
  • process.exit() on the parent (Should not kill the child)
  • Child binds the listening sockets again

Could this work or am I totally wrong?

This is possible, but generally people just use existing solutions that start a single master process and N server processes. Then when a server process dies or stops, it just starts a new one. One example is pm2.

That said, your example sounds correct to me as long as you use the detached: true argument for .spawn