Symlink – Roll to New File Without Affecting Open Handles

logssymlinktail

An application I am developing locally logs it's output to files formatted with the current timestamp such as app-%Y%m%d.log.

To make it simple to be able to tail the current's day log in a terminal window, I have a symlink named current.log which points to today's log.

At the start of work each day, I need to kill the tail process, point the symlink at today's file, and then re-run the command to tail -f current.log.

Is it possible to change the target of the symlink without having to restart tail – by changing the target of the file handle without tail being any wiser?

To automate this "start of new work day" task it would be easy to setup a cron'ed script to point the symlink at today's file, but it seems that the existing tail process would have no idea that the target has changed.

Best Answer

I don't think that is possible, unless you can get tail to close and reopen the file periodically. One tail (or any other program) opens a file, it gets a handle to the inode of that file. At that point, filenames and links are no longer consulted. That is why you can delete a file from the filesystem, and any program that has that file open will continue to work. Its is only when the ;ast program closes the file that it actually gets removed from disk.

Update: At least the version tail on OS X has a -F option, which will reopen the file if it has moved.

Related Question