node fork.js
Worker PID: 43248
Worker PID: 43249
Worker PID: 43250
Worker PID: 43251
Worker PID: 43252
const | |
fs = require('fs'), | |
child_process = require('child_process'); | |
var workers = []; | |
for(var i=0; i<5; i++) { | |
var worker = child_process.fork("lib/ExportWorker.js", [i]); | |
workers.push(worker); | |
console.log('Worker PID: ' + worker.pid); | |
worker.on('error', function (err) { | |
console.log('Exit on error: ', err); | |
}); | |
worker.on('close', function (code) { | |
console.log('Child process (' + worker.pid + ') exited with code: ' + code); | |
}); | |
} | |
process.on('SIGHUP',function(){ | |
for(var i=0; i < workers.length; i++){ | |
console.log('Sending SIGHUP to child process: ' + workers[i].pid) | |
workers[i].kill('SIGHUP'); | |
} | |
}); | |
process.on('SIGTERM',function(){ | |
for(var i=0; i < workers.length; i++){ | |
console.log('Sending SIGTERM to child process: ' + workers[i].pid) | |
workers[i].kill('SIGTERM'); | |
} | |
process.exit(1); | |
}); |
ps aux |grep [n]ode
kpettijohn 43252 0.0 0.3 3113020 54716 s009 S+ 7:06PM 0:00.40 /usr/local/bin/node lib/ExportWorker.js 4
kpettijohn 43251 0.0 0.3 3113020 54680 s009 S+ 7:06PM 0:00.39 /usr/local/bin/node lib/ExportWorker.js 3
kpettijohn 43250 0.0 0.3 3113020 54708 s009 S+ 7:06PM 0:00.39 /usr/local/bin/node lib/ExportWorker.js 2
kpettijohn 43249 0.0 0.3 3113020 54808 s009 S+ 7:06PM 0:00.39 /usr/local/bin/node lib/ExportWorker.js 1
kpettijohn 43248 0.0 0.3 3113020 54752 s009 S+ 7:06PM 0:00.39 /usr/local/bin/node lib/ExportWorker.js 0
kpettijohn 43247 0.0 0.1 3054064 14288 s009 S+ 7:06PM 0:00.05 node fork.js
Nice! Updated with your changes!