Created
March 3, 2011 19:19
-
-
Save stagas/853328 to your computer and use it in GitHub Desktop.
respawns app if it dies
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
// mit licenced | |
var sys = require('util') | |
, child_process = require('child_process') | |
, proc | |
process.title = 'nodie' | |
;(function respawn(app) { | |
console.log('Starting', app) | |
try { | |
proc = child_process.spawn(app) | |
} catch(e) { | |
return setTimeout(function() { | |
respawn(app) | |
}, 5000) | |
} | |
proc.stdout.on('data', function (data) { | |
process.stdout.write(data) | |
}) | |
proc.stderr.on('data', function (data) { | |
sys.print(data) | |
}) | |
proc.on('exit', function (err, sig) { | |
if (err) { | |
console.log('Process exited with error:', err, sig) | |
console.log('Restarting in 5 seconds') | |
setTimeout(function() { | |
respawn(app) | |
}, 5000) | |
} else { | |
console.log('Process exited gracefully') | |
console.log('Restarting in 5 seconds') | |
setTimeout(function() { | |
respawn(app) | |
}, 5000) | |
} | |
}) | |
}(process.argv[2])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment