How to Save Top Command Output to a File
To Save Top Command Output to a File
Linux top command is highly used by system administrators to display system statistics in real time regarding system uptime and load average, used memory, running tasks, a summary of processes or threads and detailed information about each running process.
Saving Top Command Output
First, we will redirect the output of top command to top.txt file in the current working directory by running the following command.
root@linuxhelp:~# top -b -n 1 > top.txt
And then, read the resulted file, use a command line file reader utility, such as cat command, less or more as follows.
root@linuxhelp:~# less top.txt
top - 05:27:38 up 1:09, 1 user, load average: 0.36, 0.12, 0.11
Tasks: 228 total, 1 running, 227 sleeping, 0 stopped, 0 zombie
%Cpu(s): 3.7 us, 1.9 sy, 1.2 ni, 91.8 id, 1.3 wa, 0.0 hi, 0.1 si, 0.0 st
KiB Mem : 2029876 total, 331784 free, 743740 used, 954352 buff/cache
KiB Swap: 2094076 total, 2091308 free, 2768 used. 1074884 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
7801 root 20 0 41800 3832 3224 R 5.9 0.2 0:00.02 top
1 root 20 0 119744 5944 4040 S 0.0 0.3 0:02.69 systemd
.
.
After that, you need to grab five iteration of top command, execute the command as shown below.
root@linuxhelp:~# top -b -n 5 > top-5iterations.txt
In order to display only the number of running tasks from the resulted file, use the grep filter, as shown in the below command example.
root@linuxhelp:~# cat top-5iterations.txt | grep Tasks
Tasks: 226 total, 1 running, 225 sleeping, 0 stopped, 0 zombie
Tasks: 226 total, 2 running, 224 sleeping, 0 stopped, 0 zombie
Tasks: 226 total, 1 running, 225 sleeping, 0 stopped, 0 zombie
Tasks: 226 total, 2 running, 224 sleeping, 0 stopped, 0 zombie
Tasks: 226 total, 1 running, 225 sleeping, 0 stopped, 0 zombie
To take a snapshot of a specific process in top utility, execute command with the PID (-p) flag. To get the PID of a running process, issue pidof command against the name of the running process. In this example we’ ll monitor the cron process via top command by taking three snapshots of the PID.
root@linuxhelp:~# pidof crond
root@linuxhelp:~# top -p 678 -b -n3 > cron.txt
root@linuxhelp:~# cat cron.txt
top - 05:33:52 up 1:13, 1 user, load average: 0.60, 0.20, 0.13
Tasks: 0 total, 0 running, 0 sleeping, 0 stopped, 0 zombie
%Cpu(s): 3.6 us, 1.8 sy, 1.1 ni, 92.2 id, 1.2 wa, 0.0 hi, 0.1 si, 0.0 st
KiB Mem : 2029876 total, 328304 free, 747112 used, 954460 buff/cache
KiB Swap: 2094076 total, 2091308 free, 2768 used. 1071512 avail Mem
Using for iteration loop, we can display a process statistics via its PID, each two seconds, as shown in the below example. The output of the loop can also be redirected to a file.
root@linuxhelp:~# for i in {1..4} do sleep 2 & & top -b -p 678 -n1 | tail -1 done
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
Now, you shall redirect loop output to file.
root@linuxhelp:~# for i in {1..4} do sleep 2 & & top -b -p 678 -n1 | tail -1 done > > cron.txt
root@linuxhelp:~# cat cron.txt
top - 05:33:52 up 1:13, 1 user, load average: 0.60, 0.20, 0.13
Tasks: 0 total, 0 running, 0 sleeping, 0 stopped, 0 zombie
%Cpu(s): 3.6 us, 1.8 sy, 1.1 ni, 92.2 id, 1.2 wa, 0.0 hi, 0.1 si, 0.0 st
KiB Mem : 2029876 total, 328304 free, 747112 used, 954460 buff/cache
KiB Swap: 2094076 total, 2091308 free, 2768 used. 1071512 avail Mem
These are just a few examples on how you can monitor and gather system and process statistics via top command.
Comments ( 0 )
No comments available