node.js spawned process sticks around if script is killed
Not very labor friendly, but this serves as the template I use:
var spawn = require("child_process").spawn;var workers = [];var killWorkers = function() { workers.forEach(function(worker) { process.kill(worker); });});process.on("uncaughtException", killWorkers);process.on("SIGINT", killWorkers);process.on("SIGTERM", killWorkers);var new_worker = spawn("tail", ["-f", "/dev/null"]);workers.push(new_worker);
I primarily use this for killing workers in a cluster when the master process dies. Ed: Obviously, you could just call killWorkers
from anywhere, not just crashes or interrupts.