11

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.

RalfFriedl
  • 8,981

3 Answers3

18

Use less --follow-name if your version of less supports it.
That option was introduced in version 416.

Then do a normal follow command Shift+F within less.

rogerdpack
  • 1,715
6

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
Volker Siegel
  • 17,283
2

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
slm
  • 369,824
  • I strongly dislike Sun OS sometimes... – Alex Chamberlain May 01 '13 at 08:39
  • I agree, I worked for years, it drives you nuts that the tooling is like 10 years old for some of the apps. Makes no sense. This site was invaluable for keeping your sanity on Solaris: http://www.sunfreeware.com/introduction.html – slm May 01 '13 at 08:43
  • if you want more sanity, check out pkgsrc.org :) – sendmoreinfo May 01 '13 at 09:06
  • That won't work nicely. Because less will hang if you do "G" pr "F". Which you can interrupt by doing "Ctrl-C", but then it kills tail. You can then immune tail to Ctrl-C, but it's still not very usable. – Stéphane Chazelas May 01 '13 at 09:47
  • With regards to GNU tail: Check out this for information about what tools should be available on any Solaris host. (Actually GNU tail is there by default in Solaris 11). Solaris sysadmins often make it harder for their users because they leave the install at the very bare-bone install while GNU tools for Solaris are actually these days available directly from Oracle or in some cases part of default install. No reason not to make it part of your install. No reason to go to 'unofficial' repos. See link. – unixhacker2010 Jul 30 '13 at 20:19