How can I stop following output in less, without affecting the command generating the output?
I am running a server and the log of this server gets logged in terminal, not in any file. I would like to use less to view this log.
node server.js | less
When I just want to see the log, I use press Shift+F to got to end of file and keep watching logs. When I want to stop it, I use CTRL+C.
Unfortunately, this stops the server as well. I just want to stop watching the end of log without stopping the server.
How to do this?
Solution 1:
I can't find any way of exiting the F
mode in less
, so you will have to use a workaround. For example, save the output to a tmp file and then watch that tmpfile instead:
node server.js > tmpfile & less tmpfile
The &
makes the node.js
command run in the background. This means that less tmpfile
will start immediately and will be tracking the tmpfile.
Once in less
, you can press F to enter follow mode but now Ctrl+C won't kill the server, it will only stop the follow. You can now scroll around as you would like and can hit F to resume following.
Solution 2:
It seems that you are normally not supposed to exit "Forward forever" mode, which is how the manual man less
names the mode you enter when pressing Shift+F.
However, I found a little dirty trick how you can return to normal anyway. It will briefly freeze the command though, so I am not sure if it would be suitable for your server which should probably run uninterruptedly.
Anyway, here is the trick:
I assume that you have started node server.js | less
alredy and pressed Shift+F to enter "Forward forever" mode. Now less
is not reacting to any keypresses any more.
In this state, you could press Ctrl+C to kill the server process and be able to quit less
afterwards by pressing Q (which will however for some reason leave the command as stopped process in your jobs list - you have to run fg
to continue it and let it completely terminate afterwards), but this is not what we want.
Instead, you can also press Ctrl+Z to stop ("freeze") the command and return to your shell prompt. Now quickly type the shell command fg
("foreground") to let the command continue running in foreground. Note that your node
server process is also paused during this short time, you have to consider whether this is acceptable or not.
So now less
is running in foreground again as before, right? Yes, but magically it is no longer in "Forward forever" mode. You can use e.g. the arrow keys again to scroll up and down.
Unfortunately, less
seems to have stopped updating its buffer completely, you can only scroll down to the line at which you froze the command earlier, not any further. The node
server is still running and producing output though, we just have to get less
to refresh again.
The easiest way I found to do that is to simply open less
' help screen and close it again, by pressing the keys H and Q sequentially. Now everything seems to be working fine again.
The cleanest solution however is probably to follow terdon's answer and redirect the output to a temp file, using less
to monitor the file.