Skip to content

Instantly share code, notes, and snippets.

@luozhihua
Last active August 7, 2022 06:21
Show Gist options
  • Save luozhihua/7018285 to your computer and use it in GitHub Desktop.
Save luozhihua/7018285 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);
});
}
@Raidus
Copy link

Raidus commented Jan 27, 2019

What are doing within the delete function?

@Code8525
Copy link

What are doing within the delete function?

@ongiao
Copy link

ongiao commented Jun 16, 2022

What are doing within the delete function?

@o-az
Copy link

o-az commented Jul 6, 2022

What are doing within the delete function?

@danielmoncada
Copy link

What are doing within the delete function?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment