• Categories
    Category
  • Categories
    Category
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial Comments FAQ Related Articles

How to Save Top Command Output to a File

2331

 

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.
 

Tags:
caden
Author: 

Comments ( 0 )

No comments available

Add a comment

Frequently asked questions ( 5 )

Q

Is there any other device for top command?

A

iotop can be used for that

Q

I want few tools that should display the output in graphical way?

A

Use gtop,htop for this

Q

What for ntopng is used?

A

It is used for bandwidth monitoring in very efficient way

Q

How to scroll the top command?

A

"top -b -n 1" gives a static print out of the top command to the scrollable termnial

Q

How to export the output of 'top' command on hourly basis?

A

Use the following command "top >> monitor.log" & create a cron for every I hour

Related Tutorials in How to Save Top Command Output to a File

Related Tutorials in How to Save Top Command Output to a File

How to Save Top Command Output to a File
How to Save Top Command Output to a File
Feb 6, 2018
top Command in Linux with Examples
top Command in Linux with Examples
Mar 22, 2016
How to install Sqtop on Ubuntu 18.04
How to install Sqtop on Ubuntu 18.04
Oct 22, 2018
How to launch Webtop GUI by using Docker image on Ubuntu 21.04
How to launch Webtop GUI by using Docker image on Ubuntu 21.04
May 5, 2022
How to Install ntopNG on Ubuntu 21.04
How to Install ntopNG on Ubuntu 21.04
Feb 8, 2022

Related Forums in How to Save Top Command Output to a File

Related Forums in How to Save Top Command Output to a File

Top command
lucas class=
Various options in Top command
Feb 15, 2017
Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help Luk Van De Looverbosch ?
How to create a root ?

Hello,
How to create root@linuxhelp in Linux Mint 20.1 64-bit ?
Thanks in advance for your reply.
Best regards.

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.