5

I was reading about the top command and I came across something I don't quite understand:

top options:

-b Run in batch mode. This is useful for sending output from top to other programs or to a file. It executes the number of iterations specified with the -n option and terminates.

What is batch mode?

Otherwise :

How -b option is useful in the following command which iterates 5 times:

top -b -n 5 > file1

What is the difference between the command above and the following command which get rid of -b option:

top -n 5 > file1
  • 2
    You have not even compared the file contents...? :-S – Hauke Laging Jun 21 '14 at 21:12
4

Batch mode refers to batch processing, which means automated processing, without human intervention. Batch is the opposite of interactive.

In batch mode, top produces output that's more sensible for collecting to a log file or for parsing (though top isn't really good at producing parseable output even in batch mode). There is no limit on the number of output lines and the output doesn't contain any escape sequences for formatting.

In interactive mode, top produces output intended for human viewing. In particular, it only displays one screenful of data. The output contains some escape sequences for formatting. Top operates in interactive mode even when its output is redirected to a file; only the presence of the -b option matters.

4

1. Batch mode is useful if you want to save monitored data for later review or to analyze it with another program. You can use the collected data to generate custom log files (an example usage is described here), databases or even graphs that can be uploaded to a web server for remote monitoring. You can find batch output in many popular programs like the snort intrusion detection system.

2. The -b option tells top to use batch mode, which is machine readable. This means you don't get the fancy formatting (which you see while running top in interactive mode), but all data will be included. If you redirect top -n 5 to a file, you will only see those lines that fit on your screen. This is because top assumes that it is being run interactively and expects the user to scroll down if he wants to see all the lines. Try it yourself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.