Created
October 10, 2013 05:56
-
-
Save supersha/6913695 to your computer and use it in GitHub Desktop.
nohup run node.js
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 fs = require('fs'), | |
exec = require('child_process').exec, | |
isWin = process.platform.indexOf('win') === 0; | |
function nohup(cmd, options, callback){ | |
if(typeof options === 'function'){ | |
callback = options; | |
options = null; | |
} | |
if(isWin){ | |
var cmdEscape = cmd.replace(/"/g, '""'), | |
file = '.nohup.cmd.vbs', | |
script = ''; | |
script += 'Dim shell\n'; | |
script += 'Set shell=Wscript.CreateObject("WScript.Shell")\n'; | |
script += 'shell.Run "cmd.exe /c start /b ' + cmdEscape + '", 0, TRUE'; | |
fs.writeFileSync(file, script); | |
exec('cscript.exe /nologo "' + file + '"', options, function(){ | |
fs.unlinkSync(file); | |
if(callback) callback(); | |
}); | |
} else { | |
exec('nohup ' + cmd + ' > /dev/null 2>&1 &', options, callback); | |
} | |
} | |
nohup('node index.js', function(){ | |
console.log('done'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment