Best way to switch between top and tail -f

60 Views Asked by At

I need to monitor both CPU usage and a specific log file and switch between them regularly.

Is there a better way than Ctrl+C, up-arrow, up-arrow, Enter? (And optionally also re-configuring top each time to sort by mem)

Ideally, with just one key stroke.

I know I can workaround with two terminal windows and Alt-Tab, but I have to also do a lot of other work and having to juggle that extra window is inconvenient.

1

There are 1 best solutions below

6
mrrobot.viewsource On

One way to achieve this is by using infinite while loop.

Code:

while : ;do top -n 5 -o %MEM; timeout 5 tail -f /path/to/your_log.log; done

Explanation:

top -n 5 - Allows you to specify after how many refreshes (Default refresh time is 3 seconds.) should top command quit. (The top command will exit after 5 refreshes in this example)
top -o %MEM - To sort by memory usage.
timeout 5 - timeout exits command after given interval. (5 seconds in this example).

You can change the time interval of top and timeout as per your requirement.

To break out of the loop you can use Ctrl+C.

Hope this helps!