Skip to content

Instantly share code, notes, and snippets.

@stagas
Created March 3, 2011 19:19
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 stagas/853328 to your computer and use it in GitHub Desktop.
Save stagas/853328 to your computer and use it in GitHub Desktop.
respawns app if it dies
// 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