Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
    
Maybe you should check this answer. unix.stackexchange.com/a/53704/41104 –  Braiam Jul 18 '13 at 2:07
    
Thank you for your reply. Unfortunately there is no solution available. –  geledek Jul 18 '13 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 '13 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 '13 at 4:02
    
It will be much better but I am afraid it is beyond my reach :( –  geledek Jul 18 '13 at 6:28
show 2 more comments

1 Answer

up vote 2 down vote accepted

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)
$ tail -F $CURRENTDATE

$ 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.

share|improve this answer
    
Thank you for your explanation! –  geledek Jul 18 '13 at 6:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.