In Linux, the “sar” (System Activity Report) command is utilized for generating reports of various system activities. These activities include CPU utilization, memory utilization, block devices, etc. It also displays the average of statistics since the system started. The user can also save the output into a file for keeping the record.
This post will present the working, usage, and practical implementation of the sar command in Linux.
- Examples of the sar Command in Linux
Before getting into details, the following pre-requisites are necessary to follow:
Install the sar Command
The sar command is not pre-installed in the operating system, you have to install it manually. Use the following command in the terminal according to your Linux distribution:
$ sudo apt install sysstat #For Debian/Ubuntu $ sudo yum install sysstat #CentOS/Fedora/RHEL/Rocky Linux/Alma Linux
The sar command will be installed.
Enable sar For Fetching Activities of the System
In order to fetch the activities of the system, enable it from the file “/etc/default/sysstat” file. Use the nano editor with sudo permission to edit the file and change the “Enable = false” to “Enable = true ”:
$ sudo nano /etc/default/sysstat
Save the file by pressing “Ctrl+O” and exit from the by pressing “Ctrl+X”
Once you made the changes in the file, restart the “sysstat” service by running the below command:
$ systemctl restart sysstat.service
The service has been restarted.
Examples of the sar Command in Linux
The syntax for using the sar command is given below.
$ sar [options] [interval] [count]
Use the sar keyword, “options” for the sar command, “interval” for a specific time, and the “count” for the output.
Let’s implement the sar command in the below examples.
Example 1: Retrieving CPU Utilization
To retrieve the CPU utilization of the system, use the “u” flag, and give the time interval, and the number of counts.
We have given the time interval of 2 and number count 4, i.e., it will display CPU utilization 4 times for every 2 seconds:
$ sar -u 2 4
The CPU utilization has been printed.
Example 2: Retrieve Memory Utilization
To retrieve the memory utilization, use the “r” flag and give the time interval and the number of counts:
$ sar -r 2 5
The memory utilization of every 2 seconds has been printed 5 times.
Example 3: Retrieve Report of Paging Statistics
To retrieve the paging statistics report, utilize the “B” flag in the command and give time interval and count:
$ sar -B 2 4
The paging statistics report of the system has been printed.
Example 4: Retrieve the Statistics Report For Particular Processor
To retrieve the statistics report for the particular processor (0 for Processor 1, 1 for processor 2…), use the “P” flag:
$ sar -P 0 2 5
The Particular processor (first processor) statistics report has been printed.
Example 5: Retrieve the Report of Memory Swapping Statistics
To Retrieve the memory swapping statistics report use the “W” flag in the command:
$ sar -W 2 4
The memory swapping report for every 2 seconds has been printed 4 times as given in the command.
Example 6: Retrieve the Report of Block Devices
To retrieve the report of block devices, use the “d” flag and “p” flag for printing the output in more readable:
$ sar -d -p 2 4
The block devices statistics report has been printed.
Example 7: Retrieve the Report of Network Statistics
To retrieve the report of the network statistics, use the “n” flag and specify the network name in the command:
$ sar -n DEV 2 2
The network report for the “Dev” has been printed.
Example 8: Retrieve the Report of File System Statistics
To retrieve the report of the file system, use the “F” flag in the command:
$ sar -F 2 2
The file system statistics report has been printed.
Example 9: Retrieving Report of Kernel Table
The Kernel table holds the entry of processes executed in the operating system. To retrieve the report of the Kernel table, utilize the “v” flag in the command.
$ sar -v 2 3
The Kernel table report has been printed.
Example 10: Save the System Report to a File
The user can save any of the system’s reports to a file through the redirection operator. Run the command along with the redirection operator, and specify the file name.
Check the execution of the below command in which we are storing the CPU utilization report in the “store.txt” file:
$ sar -u 2 4 > store.txt
Let’s check the output of the “store.txt” file:
$ cat store.txt
The file contains the report of the above-executed command.
In Linux, the sar command is utilized for retrieving the report of the various system activities such as CPU utilization, memory utilization, etc. The user can also save any output to the file through the redirection operator This write-up has covered the various examples of the sar command.