How to Run Top in Batch Mode

Top is a handy little tool that allows you to view frequently updated information about your Linux server from load average to free memory. By default, top refreshes every three seconds, giving you near real-time monitoring of CPU and memory usage, top running processes, and more.

Normally, you need to run top from the console, terminal, or remote connect (via SSH) in order to view its information, but there is another way to receive top’s information without needing to run it in the foreground. With this method, you can log top’s activity to a file for later use.

Batch mode, designated by the “-b” option, tells top to run without any interactive measures required by the user. You can then send that information to a file using a method such as this:

top -b -d 10 -n 3 >> top-results

In this example, “-d” means top will refresh every 10 seconds, and “-n” means there will be a total of 3 iterations. You can adjust these options to suit your needs. Finally, “>>” will feed the output from top to a file named “top-results”, which you can view at any time, giving you your own top log file.