8

Say I have the following Node program, a machine that goes "Ping!":

var machine = require('fs').createWriteStream('machine.log', {
    flags    : 'a',
    encoding : 'utf8',
    mode     : 0644
});

setInterval(function () {
    var message = 'Ping!';
    console.log(message);
    machine.write(message + '\n');
}, 1000);

Every second, it will print a message to the console and also append it to a log file (which it will create at startup if needed). It all works great.

But now, if I delete the machine.log file while the process is running, it will continue humming along happily, but the writes will no longer succeed because the file is gone. But it looks like the writes fail silently, meaning that I would need to explicitly check for this condition. I've searched the Stream docs but can't seem to find an obvious event that is emitted when this type of thing occurs. The return value of write() is also not useful.

How can I detect when a file I'm writing to is deleted, so I can try to reopen or recreate the file? This is a CentOS box, if that's relevant.

1 Answer 1

9

The writes actually do not fail.

When you delete a file that is open in another program you are deleting a named link to that file's inode. The program that has it open still points to that inode. It will happily keep writing to it, actually writing to disk. Only now you don't have a way to look it at, because you deleted the named reference to it. (If there were other references, e.g. hard links, you would still be able to!).

That's why programs that expect their log files to "disappear" (b/c of logrotate, say) usually support a signal (usually SIGHUP and sometimes SIGUSR1) that tells them to close their file (at which point it is really gone, because now there are no links to it anywhere) and re-create it.

You should consider something like that as well.

5
  • Ah, that makes sense. I've seen that before, but it never occurred to me that it was the same sort of behavior happening. I wonder what would happen if a more serious problem made the file actually unwritable (disk failure, etc.) -- would the node process get a notification or would the whole thing just fall over?
    – smitelli
    Oct 11, 2013 at 17:03
  • The underlying OS will let the node process with an appropriate errno. Whether or not this will go all the way from node's io layer up to the Stream class and then all the way up to the application I am not sure. I am assuming that it will. Oct 11, 2013 at 20:24
  • from doc: SIGUSR1 is reserved by Node.js to start the debugger. It's possible to install a listener but doing so might interfere with the debugger.
    – bato3
    Mar 7, 2019 at 11:24
  • Or use fsPromises.watch or chokidar multiplatform wrapper
    – xmedeko
    May 3, 2021 at 14:50
  • Or use fsPromises.writeFile which is slower than stream but robust to file deletion.
    – xmedeko
    May 3, 2021 at 14:51

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.