less with "update file" like functionality

Solution 1:

You can get this effect by issuing the F command (Shift+F) while viewing the file in less. To stop following and switch back to paging, press Ctrl+C

Since your file only changes every 5 minutes, you could also use tail -f and specify a longer sleep time with -s (defaults to 1 second). For instance,

tail -f -s 60 myfile

checks myfile for output every 60 seconds.

EDIT: Due to misleading question, the above answer was unsatisfactory. Second attempt follows:

To re-open the same file in less every 5 minutes, try this:

while true; do ( sh -c 'sleep 600 && kill $PPID' & less myfile ); done

This will spawn a subshell which backgrounds another shell process instructed to kill its parent process after 5 minutes. Then it opens the file with less. When the backgrounded shell command kills the parent subshell, it kills all its children, including the "less" command. Then the loop starts the process over again.

The only easy way I know of to kill this is to kill the terminal your original shell is in. If that's unacceptable, you can use the "killfile" trick:

touch killfile
while [ -f killfile]; do stuff; done

To stop doing stuff, rm the killfile in another shell.

Solution 2:

You can do something similar in vim.

Start a server session of vim:

vim -R --servername refresh_session

Then in another console, monitor the file for updates and tell the vim session to reload the file as soon as it gets updated:

inotifywait -e close_write -m your_log_file | while read filename events; do
    vim --servername refresh_session --remote $filename
done

A few gotchas.

  • This will of course not work if your vim is not compiled with the clientserver feature in.
  • inoifywait will stop working when the file gets deleted. So I hope your file gets overwritten. It is possible to work around that, too, of course.

And if you want to have a more less-like experience, you can use the less macros to get your less key bindings in vim.

/usr/share/vim/vim73/macros/less.sh --servername refresh_session