Tailing Rolling Files

前端 未结 2 1738
别跟我提以往
别跟我提以往 2021-02-13 04:02

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 2         


        
2条回答
  •  遇见更好的自我
    2021-02-13 04:31

    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!)

提交回复
热议问题