{"id":12137,"date":"2019-03-22T11:42:41","date_gmt":"2019-03-22T11:42:41","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=12137"},"modified":"2019-03-22T11:42:41","modified_gmt":"2019-03-22T11:42:41","slug":"how-to-save-top-command-output-to-a-file","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/22\/how-to-save-top-command-output-to-a-file\/","title":{"rendered":"How to Save Top Command Output to a File"},"content":{"rendered":"<p>Linux\u00a0<a href=\"https:\/\/www.tecmint.com\/12-top-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">top command<\/a>\u00a0is highly used by system administrators to display system statistics in real time regarding\u00a0<strong>system uptime<\/strong>\u00a0and\u00a0<strong>load average<\/strong>,\u00a0<strong>used memory<\/strong>,\u00a0<strong>running tasks<\/strong>, a summary of processes or threads and detailed information about each running process.<\/p>\n<p>However, besides real time viewing of the running system,\u00a0<a href=\"https:\/\/www.tecmint.com\/12-top-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">top command<\/a>\u00a0output can be saved to a file, by using the\u00a0<code>-b<\/code>\u00a0flag, which instructs\u00a0<a href=\"https:\/\/www.tecmint.com\/find-processes-by-memory-usage-top-batch-mode\/\" target=\"_blank\" rel=\"noopener\">top to operate in batch mode<\/a>\u00a0and\u00a0<code>-n<\/code>\u00a0flag to specify the amount of iteration the command should output.<\/p>\n<p>In the below example, we\u2019ll redirect the output of\u00a0<strong>top command<\/strong>\u00a0to\u00a0<code>top.txt<\/code>\u00a0file in the current working directory. The\u00a0<strong>-n<\/strong>\u00a0argument will be used to send only one snapshot of the command to the mentioned file.<\/p>\n<pre>$ top -b -n 1 &gt; top.txt\r\n<\/pre>\n<p>To read the resulted file, use a command line file reader utility, such as\u00a0<a href=\"https:\/\/www.tecmint.com\/13-basic-cat-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">cat command<\/a>,\u00a0<a href=\"https:\/\/www.tecmint.com\/linux-more-command-and-less-command-examples\/\" target=\"_blank\" rel=\"noopener\">less or more<\/a>.<\/p>\n<pre>$ less top.txt\r\n<\/pre>\n<div id=\"attachment_28460\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/01\/View-Output-of-Top-Command.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-28460\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/01\/View-Output-of-Top-Command.png\" sizes=\"auto, (max-width: 830px) 100vw, 830px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/01\/View-Output-of-Top-Command.png 830w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/01\/View-Output-of-Top-Command-768x415.png 768w\" alt=\"View Output of Top Command\" width=\"830\" height=\"448\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">View Output of Top Command<\/p>\n<p>To grab five iteration of\u00a0<strong>top command<\/strong>, execute the command as shown in the below excerpt.<\/p>\n<pre>$ top -b -n 5 &gt; top-5iterations.txt\r\n<\/pre>\n<p>In order to display only the number of running tasks from the resulted file, use the\u00a0<a href=\"https:\/\/www.tecmint.com\/12-practical-examples-of-linux-grep-command\/\" target=\"_blank\" rel=\"noopener\">grep filter<\/a>, as shown in the below command example.<\/p>\n<pre>$ cat top-5iterations.txt | grep Tasks\r\n<\/pre>\n<div id=\"attachment_28461\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/01\/Show-Top-5-Running-Tasks.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-28461\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/01\/Show-Top-5-Running-Tasks.png\" alt=\"Show Top 5 Running Tasks\" width=\"700\" height=\"152\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Show Top 5 Running Tasks<\/p>\n<\/div>\n<p>To take a snapshot of a specific process in\u00a0<strong>top utility<\/strong>, execute command with the\u00a0<strong>PID<\/strong>\u00a0<code>(-p)<\/code>\u00a0flag. To get the\u00a0<a href=\"https:\/\/www.tecmint.com\/find-process-name-pid-number-linux\/\" target=\"_blank\" rel=\"noopener\">PID of a running process<\/a>, issue\u00a0<strong>pidof command<\/strong>\u00a0against the name of the running process.<\/p>\n<p>In this example we\u2019ll monitor the\u00a0<strong>cron<\/strong>\u00a0process via\u00a0<strong>top command<\/strong>\u00a0by taking three snapshots of the PID.<\/p>\n<pre>$ pidof crond\r\n$ top -p 678 -b -n3 &gt; cron.txt\r\n$ cat cron.txt\r\n<\/pre>\n<div id=\"attachment_28462\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/01\/Take-Screenshot-of-Process-in-Top.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-28462\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/01\/Take-Screenshot-of-Process-in-Top.png\" sizes=\"auto, (max-width: 810px) 100vw, 810px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/01\/Take-Screenshot-of-Process-in-Top.png 810w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/01\/Take-Screenshot-of-Process-in-Top-768x482.png 768w\" alt=\"Watch Process Usage in Top Command\" width=\"810\" height=\"508\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Watch Process Usage in Top Command<\/p>\n<\/div>\n<p>Using a\u00a0<strong>for<\/strong>\u00a0iteration 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. We\u2019ll use the same cron PID as shown in the above example.<\/p>\n<pre>$ for i in {1..4}; do sleep 2 &amp;&amp; top -b -p 678 -n1 | tail -1 ; done\t\r\n<\/pre>\n<p>Redirect loop output to file.<\/p>\n<pre>$ for i in {1..4}; do sleep 2 &amp;&amp; top -b -p 678 -n1 | tail -1 ; done &gt;&gt; cron.txt\r\n$ cat cron.txt\r\n<\/pre>\n<div id=\"attachment_28463\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/01\/Find-Linux-Process-Statistics.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-28463\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/01\/Find-Linux-Process-Statistics.png\" sizes=\"auto, (max-width: 997px) 100vw, 997px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/01\/Find-Linux-Process-Statistics.png 997w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/01\/Find-Linux-Process-Statistics-768x189.png 768w\" alt=\"Find Linux Process Statistics\" width=\"997\" height=\"246\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Find Linux Process Statistics<\/p>\n<\/div>\n<p>These are just a few examples on how you can monitor and gather system and process statistics via\u00a0<strong>top command<\/strong>.<\/p>\n<p><a style=\"font-size: 1rem;\" href=\"https:\/\/www.tecmint.com\/save-top-command-output-to-a-file\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Linux\u00a0top command\u00a0is highly used by system administrators to display system statistics in real time regarding\u00a0system uptime\u00a0and\u00a0load average,\u00a0used memory,\u00a0running tasks, a summary of processes or threads and detailed information about each running process. However, besides real time viewing of the running system,\u00a0top command\u00a0output can be saved to a file, by using the\u00a0-b\u00a0flag, which instructs\u00a0top to operate &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/22\/how-to-save-top-command-output-to-a-file\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Save Top Command Output to a File&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-12137","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12137","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=12137"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12137\/revisions"}],"predecessor-version":[{"id":12138,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12137\/revisions\/12138"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=12137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=12137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=12137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}