My node process is dying and I can't seem to log to a file when the process exits. It is a long running process invoked directly with node index.js:
// index.js
const fs = require('fs');
exports.getAllCars = (process => {
if (require.main === module) {
console.log(`Running process: ${process.getgid()}.`);
let out = fs.createWriteStream(`${__dirname}/process.log`);
// trying to handle process events here:
process.on('exit', code => out.write(`Exit: ${code}`));
return require('./lib/cars').getAllCars();
} else {
return require('./lib/cars').getAllCars;
}
})(process);
Also tried creating event handlers for error, uncaughtException. Nothing works when killing my process manually (with kill {pid}). The file process.log is created but nothing is there. Do writeable streams require a stream.end() to be called on completion?
kill $PIDsends a SIGTERM, so try adding a signal handler for those.kill {pid}to test. My real use-case is when the process just dies without any error logged anywhere. I haven't had this happen since I put the error handling in there. Usually the process dies unexpectedly after ~4 hrs.exit,erroranduncaughtException, the process is running fine...¯\_(ツ)_/¯