Last active
December 29, 2015 19:27
-
-
Save hellwolf/33988cda1c8aecb7ebdb to your computer and use it in GitHub Desktop.
[NodeJS] Spawn a child process and write a pid file
This file contains hidden or 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
!/usr/bin/env node | |
var spawn = require('child_process').spawn; | |
var fs = require('fs'); | |
var c = spawn(process.argv[3], process.argv.slice(4), { stdio: 'inherit' }); | |
if (c.pid) { | |
pidFile = fs.createWriteStream(process.argv[2]); | |
pidFile.write(c.pid.toString()); | |
pidFile.end(); | |
} | |
c.on("error", function(err) { | |
console.error(err); | |
}) | |
c.on("close", function(err) { | |
fs.unlink(process.argv[2], function (err) {}); | |
}); | |
# Example usage: | |
# $ ./scripts/spawn.js ./data/test.pid sleep 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment