-
-
Save ravenstar/1a858ba502874477d7189e46a9f7cce6 to your computer and use it in GitHub Desktop.
Auto restart nodejs process when program exit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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