Node.js: Detecting a file, opened with fs.createWriteStream(), becoming deleted

后端 未结 1 1727
面向向阳花
面向向阳花 2021-01-19 16:49

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

var machine = require(\'fs\').createWriteStream(\'machine.log\', {
    flags    : \'a\'         


        
相关标签:
1条回答
  • 2021-01-19 17:23

    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.

    0 讨论(0)
提交回复
热议问题