LearnBoost / cluster

Node.JS multi-core server manager with plugins support.
http://learnboost.github.com/cluster
MIT License
2.29k stars 159 forks source link

How to detach (deamonize) master process correctly? #130

Closed ybogdanov closed 13 years ago

ybogdanov commented 13 years ago

Hi,

nohup is good, but it kills the process at the end of session. I use capistrano to run commands on remote servers, e.g. start/stop/restart clusterized processes.

The quesion: what is the best way to start cluster process remotely and bring it to background?

Thanks!

ybogdanov commented 13 years ago

Temporarily, I spawn new process in new session:

var start = process.argv[process.argv.length - 1] === 'start';

if (start) {
    var node = process.execPath,
        cmd = process.argv.slice(1, -1);

    spawn(node, cmd, { env : process.env, setsid: true });
    process.exit(0);
}

// require, initialize, start cluster here...

And then $ myapp.js start

tj commented 13 years ago

yeah to achieve similar right now we would have to pretty much do exactly what you're doing

ybogdanov commented 13 years ago

Ok, thanks!

alefnula commented 13 years ago

Just a suggestion: maybe adding a command to cli? Something like this?

define('-b, --background, background', function(master) {
  master.on('listening', function () {
    master.killall('SIGUSR2');      
  });
}, 'Start master in background');

The only problem is the part:

90  if (~command.flags.indexOf(arg)) {
91    command.callback(master);
92    process.exit(0);
93  }

Because the process exists. Maybe adding a switch to command exit_after or checking the return value do_exit = command.callback(master)

tj commented 13 years ago

@alefnula yeah i know i have that there for a reason but ill see if i can use the preventDefault() stuff i added a while back

alefnula commented 13 years ago

I'm sure there is a much better way of doing it :) It was just the first thing I found that could work after a quick look at the code. But thanks for taking the cli background suggestion into account :)