Skip to content

Instantly share code, notes, and snippets.

@hellwolf
Last active December 29, 2015 19:27
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 hellwolf/33988cda1c8aecb7ebdb to your computer and use it in GitHub Desktop.
Save hellwolf/33988cda1c8aecb7ebdb to your computer and use it in GitHub Desktop.
[NodeJS] Spawn a child process and write a pid file
!/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