2

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?

4
  • kill $PID sends a SIGTERM, so try adding a signal handler for those. Commented Sep 20, 2017 at 19:43
  • Well, I was just using 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. Commented Sep 20, 2017 at 20:35
  • If it dies without any errors being logged, my guess would be there's an outside reason for the process being killed (for instance, the OOM killer on Linux). Commented Sep 21, 2017 at 6:19
  • Of course now that I have handlers in place for exit, error and uncaughtException, the process is running fine... ¯\_(ツ)_/¯ Commented Sep 22, 2017 at 14:32

2 Answers 2

7
+25

According to Node.js documentation:

The 'exit' event is emitted when the Node.js process is about to exit as a result of either:

  • The process.exit() method being called explicitly.
  • The Node.js event loop no longer having any additional work to perform.

So, if you start a process that should never end, it will never trigger.

Also, writable streams do not require to be closed:

If autoClose(an option from createWriteStream) is set to true (default behavior) on error or end the file descriptor will be closed automatically.

however, the createWriteStream function opens the file with flag 'w' by default, which means that the file will be overwritten every time (maybe this is the reason why you always see it empty). I suggest to use

fs.appendFileSync(file, data)

Here are the events that want to listen:

//catches ctrl+c event
//NOTE:
//If SIGINT has a listener installed, its default behavior will be removed (Node.js will no longer exit).
process.on('SIGINT', () => {
    fs.appendFileSync(`${__dirname}/process.log`, `Received SIGINT\n`);
    process.exit()
});

//emitted when an uncaught JavaScript exception bubbles
process.on('uncaughtException', (err) => {
    fs.appendFileSync(`${__dirname}/process.log`, `Caught exception: ${err}\n`);
});

//emitted whenever a Promise is rejected and no error handler is attached to it
process.on('unhandledRejection', (reason, p) => {
    fs.appendFileSync(`${__dirname}/process.log`, `Unhandled Rejection at: ${p}, reason: ${reason}\n`);
});
Sign up to request clarification or add additional context in comments.

Comments

-1

I suggest you put the code in a try catch block to find out whether its the code or some external cause which results in program termination. and then check the log after the event...

try {
  //your code 
}catch(e) {
  console.log(e.stack);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.