I have a task to use a bash script to monitor a couple of log files named with the current day, eg. YYYY-MM-DD.log.

(1) I used tail -f to follow the file but unable to deal file rotation.

Even if using tail -F $FILENAME, I can only follow the log for 24 hours max and need to restart again.

What I wish to accomplish is to have something like tail -F $CURRENTDATE, so I can leave it running forever and every midnight It will switch to follow the new log file named with current date.

But it seems the variable $CURRENTDATE is not dynamic. Is there another way to do this without crontab'ing the script every 24 hours? Maybe set a symbolic file or something?

(2) Meanwhile, if tail -f is used, what signal will it return if the file it follows is missing or changes its name? How can I trap the exceptions during execution.

  • Maybe you should check this answer. unix.stackexchange.com/a/53704/41104
    – Braiam
    Jul 18, 2013 at 2:07
  • Thank you for your reply. Unfortunately there is no solution available.
    – geledek
    Jul 18, 2013 at 2:47
  • I am looking into a solution provided by a Perl script from search.cpan.org/~mgrabnar/File-Tail-0.99.3/Tail.pm#___top.
    – geledek
    Jul 18, 2013 at 2:48
  • Actually, just so slm answer has validity, what process generate these logs files? And can you make it so it uses a single name instead and the rotate job add the dates?
    – Braiam
    Jul 18, 2013 at 4:02
  • It will be much better but I am afraid it is beyond my reach :(
    – geledek
    Jul 18, 2013 at 6:28

2 Answers 2


The variable $CURRENTDATE isn't dynamic the way that you think. That variable gets expanded when the tail -F $CURRENTDATE command is evaluated to execute. You can see this if you run the ps command, AFTER it's executed. Notice in the output that it shows the value of current date rather than $CURRENTDATE.

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

$ ps -eaf|grep tail
saml      1171 13564  0 22:13 pts/4    00:00:00 tail -F 20130517.log

This issue you're encountering is typically why most server daemons such as Apache, Nginx, or Jetty log all the current date into a file named, error.log, and then this file is rotated to another name, such as error_20130517.log afterwards.

This allows you to monitor the error.log continuously using a command such as tail -F error.log.


This is exactly what you are looking for:

watch -n 60 'tail -n 30 /path/to/logfile-$(date +%Y-%m-%d.log)'

This refreshes the logfile for the current date every 60 seconds. Customize the n parameters to your liking. Note the single quotes: These make the watch command evaluate the command every time instead of evaluating it once and providing output. I ran into the same problem myself and this worked perfectly for me. I don't think the current answer of "Well the big players don't like to hack around so they use logrotate" is an adequate solution if you're trying to debug a program that logs this way instead of the other way.

This is the quick and dirty way, but If you're developing something, make like the big players and use logrotate.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .