Search code examples
linuxshellnginxlogfilesgentoo

shellscript to monitor nginx log file on Gentoo expires after log file was gzip'ed


this is a follow up question of my previous question

So I have this shellscript to monitor a nginx log file on Gentoo, the problem is the log file were renamed and gziped after certain file size or period of time, the shellscript still looking up the original file-descriptor(or inode?), the new file content won't show up.

My question is, how to hook into the auto-gzip script to have my log file monitor script restarted to monitor the new logfile?

BTW Please don't do it using cronjob or using a timer. I want a near real-time response of my monitor script.


Solution

  • Use the -F flag instead of the -f flag for tail (or --follow=name will work too, though that will only work on Linux while -F will work on BSDs and Mac OS X as well):

       --retry
              keep trying to open a file even if it is inaccessible when  tail
              starts  or  if it becomes inaccessible later -- useful only with
              -f
    
       -f, --follow[={name|descriptor}]
              output appended data as the file grows; -f, --follow, and --fol-
              low=descriptor are equivalent
    
       -F     same as --follow=name --retry
    
       With  --follow  (-f),  tail  defaults to following the file descriptor,
       which means that even if a tail’ed file is renamed, tail will  continue
       to  track  its  end.   This  default behavior is not desirable when you
       really want to track the actual name of the file, not the file descrip-
       tor (e.g., log rotation).  Use --follow=name in that case.  That causes
       tail to track the named file by reopening it periodically to see if  it
       has been removed and recreated by some other program.