Skip to main content

How do I less a filename rather than an inode number? [Resolved]

I have a piece of software that rotates its log files when it restarts. However, during development, I am restarting it a lot, so I would like to monitor the latest log file at any time.

If I start less normally with less program.log and hit Shift+f to tail, when the log file is rotated, I carry on monitoring the old log file. I assume this is because the inode number stays the same and less has an open file handle to that inode.

Is it possible to monitor the latest activity on whatever log file is currently called program.log?

Specifically, I am working on Sun OS, so a solution that works there would be ideal.


Question Credit: Alex Chamberlain
Question Reference
Asked September 21, 2019
Posted Under: Unix Linux
24 views
3 Answers

The less option --follow-name is only part of the solution;
To replace tail -F, another argument is needed:

less --follow-name +F file.log

The option alone like less --follow-name file.log does not actually start following the file updates. You need to enter the follow mode be pressing ShiftF.
(Exit the mode to navigate by ControlC.)

Instead of following the file, --follow-name modifies the behaviour of less.
It makes the command key ShiftF inside of less follow based on the file name, not the file descriptor.

Also, there is no normal option to start less in follow mode.
But you can use the command line to give keystrokes to execute after startup, by prefixing them with +.
Combining the modifier option with +F, less will actually start in the (modified) follow mode.

Use +F alone for the equivalent of plain tail -f:

less +F file.log

credit: Volker Siegel
Answered September 21, 2019

I just found the answer in this U&L Q&A titled: How to do a tail -f of log rotated files?.

Using tail:

(if installing GNU tail on your system is an option)

tail -F program.log

From the tail man page:

   -f,      --follow[={name|descriptor}]
            output appended data as the file grows; -f, 
            --follow, and --follow=descriptor are equivalent

   -F       same as --follow=name --retry

   --retry  keep  trying  to  open  a  file even when it is or becomes
            inaccessible; useful when following by name, i.e., with
            --follow=name

The key is the --retry switch. This tells the tail command to keep retrying to follow a file by name. The -F switch does both a -f and a --retry.

Using less

As @StephaneChazela pointed out in the comments the following will not work.

tail -F program.log | less

The only other option you have is to use less directly assuming it supports the --follow-name switch and less the file directly, forgoing using tail completely.

less --follow-name program.log

credit: Community
Answered September 21, 2019
Your Answer
D:\Adnan\Candoerz\CandoProject\vQA