Question

I am trying to use tail -f command on a file whose name changes daily i.e. the current date is appended to its name. I tried doing this :

CURRENTDATE=$(date +%Y%m%d.log)

tail -F $CURRENTDATE

but it tails the files with the CURRENTDATE set to the time when the script was executed. Is there any way the tail command can automatically switch to the new file when the date changes ? I cannot use crontab here to execute this command every day. Basically I am using this with Flume NG exec source.

I would really appreciate some help on this.

Was it helpful?

Solution

There is no way the tail command itself can automatically switch to the new file with a different name when the date changes. Thus, you have to kill the old tail and start a new one in due time.

while
    tail -F `date +%Y%m%d.log`& sleep $(expr `date +%s -d'day 0'` - `date +%s`)
do  kill $!
done

OTHER TIPS

To tail the most recent .log file in a directory:

LASTFILE=$(ls -t *.log | head -1)

tail -F $LASTFILE

I am using flume-ng exec tailing rotated log files too. Here's my solution:

originAgent.sources.originSource2.type = exec                                                                                                                                             
originAgent.sources.originSource2.command = locktail_rotate.sh /path/to/test.DATE_ROTATE.log 'date +"%Y%m%d"'

And you can checkout locktail_rotate.sh from here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top