Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mykz/0b9fcef36f1ec3cba279d88900a4998f to your computer and use it in GitHub Desktop.
Save mykz/0b9fcef36f1ec3cba279d88900a4998f to your computer and use it in GitHub Desktop.
Auto restart nodejs process when program exit
var child_process = require('child_process');
start();
function start(nodefile) {
if (typeof start !== 'string') {
console.log('Has none file. like this: start("app.js")');
}
console.log('Master process is running.');
var proc = child_process.spawn('node', [nodefile]);
proc.stdout.on('data', function (data) {
console.log(data.toString());
});
proc.stderr.on('data', function (data) {
console.log(data.toString());
});
// 监测退出事件,删除原进程并开启新进程
proc.on('exit', function (code) {
console.log('child process exited with code ' + code);
delete(proc);
setTimeout(start, 5000);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment