How can I stop following output in less, without affecting the command generating the output?

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.


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.


As your question says:

This server gets logged in terminal, not in any file.

I assume that you don't want to use a (temp) log file, maybe your log is huge or for whatever reason you have.


Named pipe, fifo file.

What I came up with is using a named pipe file also known as fifo file, this file only will be used to pipe your logs to less, and nothing will be saved on it.

First create a fifo file:

mkfifo mylog

Run your server and redirect the logs to this file:

node server.js > mylog &

Use less to read your logs (-f forces less to read this special file):

less -f mylog

Now you can use shift+F to follow, and CTRL+C to stop the follow, but server is still running, you can follow the output again with shift+F.


Backup plan: If it stopped your server, just put your command (node server.js > mylog &) into a file e.g: server.sh, then instead of running node server.js > mylog & run: bash server.sh > mylog & and then run the less -f mylog.