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

5,905

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.

Solution 3

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.

Share:
5,905

Related videos on Youtube

Ajay Kumar
Author by

Ajay Kumar

Updated on September 18, 2022

Comments

  • Ajay Kumar
    Ajay Kumar over 1 year

    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?

    • Byte Commander
      Byte Commander about 7 years
      Do you want to exit less and return to your shell prompt with the node server running in background, or do you just want to get less back into normal non-following mode where you can scroll through the log instead of watching the latest lines?
    • Ajay Kumar
      Ajay Kumar about 7 years
      @ByteCommander when i press CTRL+C. i want my server to continue running and i want to got back to less normal non-following mode, where i can scroll.
  • terdon
    terdon about 7 years
    What's the point of using a fifo instead of a regular file here? The only benefit I can see is to save on disk space but I doubt that will be relevant for something like this. You'll still have a file there after you're done which you will have to delete manually.
  • David Foerster
    David Foerster about 7 years
    I recommend against a FIFO here because the server process may stall if the FIFO buffer runs full when nobody requests (more) log data. If log file size is becomes an issue one should really use a proper log file plus logrotate.
  • TRiG
    TRiG about 7 years
    A rather brilliant dirty hack.
  • JoL
    JoL about 7 years
    I think I prefer this answer to the accepted one. Note that you can run % rather than fg; I find it easier/quicker to type. Also it seems that F (shift-f) still works even without doing the hq trick. Nice! Now when I'm following logs, I can ctrl-z % Enter, do my less thing, then go back to following by hitting F!
  • JoL
    JoL about 7 years
    BTW, you shouldn't call it dirty. Personally, this seems cleaner than the other solutions as, here, you don't have to litter your filesystem with temporary files (for which you have to think of a name and then remove), especially when you might want to use less's follow feature in multiple terminals at the same time.
  • Ravexina
    Ravexina about 7 years
    @terdon As you said the only point is saving disk, I thought the OP doesn't want his logs to be saved on the disk.
  • Ravexina
    Ravexina about 7 years
    @DavidFoerster +1 Good Point ;)
  • alexis
    alexis about 7 years
    I haven't looked in the source, but this probably works because the TSTP signal interrupts the read system call that was waiting for new data to arrive; less receives 0 bytes and concludes that output has ended. To avoid pausing the server, open a new terminal window; execute ps and identify the process id of the less process; then type kill -TSTP <PID>; kill -CONT <PID>. This way the signals should get delivered back to back, without a delay while you're typing.
  • muru
    muru about 7 years
    @alexis if you're going to use kill, you can just send SIGINT instead. Then you won't need to send CONT, and less is just waiting for you.
  • alexis
    alexis about 7 years
    I'm pretty sure terminating less is going to cause the server to die with a "broken pipe" error (write on a pipe with nobody to read it). Do you have reason to think it won't, @muru?
  • muru
    muru about 7 years
    @alexis well, sigint won't terminate less, for the same reason Ctrl-C doesn't - it has a signal handler installed for sigint.
  • alexis
    alexis about 7 years
    Oh right, interesting suggestion. Try it, @Ajay! (However, my testing suggests that reading might not be resumed. Not sure what factors cause this.)