ralyodio / node-startup

Startup script for Linux-based systems for running node app when rebooting using an /etc/init.d script.
MIT License
779 stars 169 forks source link

Run restart from started node app? #48

Closed FossPrime closed 6 years ago

FossPrime commented 6 years ago

There doesn't seem to be a way to restart the script within node...

I can successfully kill the running app, but it won't start up again. I've tried nohup and exec

nohup makes no difference compared to running restart directly. Exec yields sudo: exec: command not found

Has anyone figured this out? I'm trying to build a webhook handler that updates the webhook handler with a git pull and app restart.

const { exec } = require('child_process');

function handler(req, res, next) {
  exec('sudo exec /etc/init.d/web-hooks restart --force', function (err, stdout, stderr) {
    console.log(err, stdout, stderr)
  })
}
FossPrime commented 6 years ago

I seem to have stumbled on a rabbit hole

My solution was to use spawn. The problem was pretending exec is just like a bash pty... it's not.

const { spawn } = require('child_process');
function handler(req, res, next) {
  spawn( 'sudo', ['/etc/init.d/web-hooks', 'restart', '--force'], { detached:true, stdio:'ignore' }).unref()
}

P.S. if you are running on a server with less than 1.5gigs in memory.. letting the interpreter know may be useful to making sure it doesn't take memory from update processes. --max_old_space_size=512. That said, I personally shut down node temporarily while updates run on the yum-cron file.

https://github.com/shelljs/shelljs/issues/426