Amar
Amar

Reputation: 3845

How can I tail a log file with the date appended to its name?

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.

Upvotes: 1

Views: 3456

Answers (3)

Yanpeng
Yanpeng

Reputation: 91

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.

Upvotes: 0

Armali
Armali

Reputation: 19385

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

Upvotes: 2

beroe
beroe

Reputation: 12316

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

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

tail -F $LASTFILE

Upvotes: 1

Related Questions