Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tailing Rolling Files

I have a directory full of rolling log files that I would like to be able to use tail on.

The files are named as such:

name      modified
00A.txt   Dec 27 19:00
00B.txt   Dec 27 19:01
00C.txt   Dec 27 19:02
00D.txt   Dec 27 19:03

On an older unix system, I'm trying to come up with a shell script that will tail the most recently modified file in a specific directory, and if that file gets administratively closed (rolls to the next file) I want the program to automatically begin tailing the new file without me having to break out of tail to rerun.

tail -100f `ls -t | head -1` 

The desired behavior, given the filenames above, would go like this:

./logtailer.sh

Then the script would begin tailing 00D.txt. As soon as the logger was finished writing to 00D.txt and the newest log file was now named 00E.txt, the program would automatically begin tailing that file.

One could write this script by watching the output of tail for the text "File Administratively Closed" and then having the following command run again.

tail -100f `ls -t | head -1`

Is there a more elegant way to do this than by watching for the text "file administratively closed"? How can I even read the output of tail line by line in a shell script?

Edit: I should explain that the -F flag for tail is not an option for me on this system. It uses a different version of tail that does not contain this feature. OS version - Solaris 10

like image 507
user1118047 Avatar asked Dec 27 '11 17:12

user1118047


People also ask

What is tailing a file?

What is the tail command? The tail command is a command-line utility for outputting the last part of files given to it via standard input. It writes results to standard output. By default tail returns the last ten lines of each file that it is given.

How do you tail a rotating log file?

To watch log files that get rotated on a daily base you can use the -F flag to tail command. The tail -F will keep track if new log file being created and will start following the new file instead of the old file.

What does it mean to tail logs?

A tail log is a log that contains recent data from a log file. There's no specific definition of how recent that data needs to be, or how many events or transactions a log tail needs to include.

Can you tail a file in Windows?

An advanced tail -f command with GUI, MakeLogic Tail is the tail for Windows. It can be used to monitor the log files of various servers and comes with a variety of other intuitive and useful features.


2 Answers

You can use the -F option for tail which implies --follow=name --retry.

From the man page:

-F      
The -F option implies the -f option, but tail will also check to see if the 
file being followed has been renamed or rotated.  The file is closed and 
reopened when tail detects that the filename being read from has a new inode 
number. The -F option is ignored if reading from standard input rather than 
a file.
like image 177
jaypal singh Avatar answered Nov 15 '22 19:11

jaypal singh


you may need inotify to detect the creation of the new file, a workaround for that would be to keep polling the filesystem while running tail on the background:

#!/bin/bash

get_latest() {
    local files=(*.log)
    local latest=${files[${#files[@]}-1]}
    echo "$latest"
    [[ $latest == $1 ]]
}
run_tail() {
    tail -c +0 -f "$1"
}

while true; do
    while current=$(get_latest "$current"); do
        sleep 1
    done
    [[ $pid ]] && kill $pid
    run_tail "$current" & pid=$!
done

(untested, unnecesarily hacky and be careful with the limitations of your old system!)

like image 45
Samus_ Avatar answered Nov 15 '22 20:11

Samus_