IN-Decent

Re-decentralizing internet with free software

Continue Following Files With tail After Recreation

Posted at — Mar 20, 2021

tail command can be used to follow files real time as data is being written to it with tail -f.

But if the file being tracked is deleted or renamed and new file is created in its place tail -f will still continue tracking old file and contents written to new files will not be displayed by tail.

This often happens in case of log files after they are automatically rotated and a new empty file is created to keep the log file size small and growing uncontrolled.

To start tracking new file created in place, you have to instruct tail to track file name instead of file descriptor by using --follow=name --retry or short form -F

Without --retry when the log file is deleted, tail will fail saying No such file or directory. --retry will instruct tail to keep trying till new file with same name appears in place.

Below command continues tailing log file even after it is rotated very midnight,

tail -F access.log | gep ' 404 '

References:

  1. tail man page