How to capture the output of a top command in a file in linux?
for me top -b > test.txt
will store all output from top
ok even if i break it with ctrl-c
. I suggest you dump first, and then grep
the resulting file.
How to capture the output of a top command (for a specific process that will die) in a file
tail -1
does not produce the last line of each screen of output that top
produces. It produces only the last line of all the lines.
Try:
top -b -n 1000 -d 5 -p "$MYPID" | grep "$MYPID" > log.txt
This will collect into log.txt
all the lines that mention the PID but none of the headers (assuming the PID doesn't appear on a header).
Note that grep
is usually buffered. This means that, if you check on log.txt
during a run, it might be out of date. With GNU grep
, you can keep log.txt
up to date with the --line-buffered
option:
top -b -n 1000 -d 5 -p "$MYPID" | grep --line-buffered "$MYPID" > log.txt
How To Capture Unix 'Top' Command Output to a CSV file?
If you want to trim runs of whitespace and replace them with commas, try
top -b -n 3 | sed -n '8, 12{s/^ *//;s/ *$//;s/ */,/gp;};12q'
The sed
script performs the following substitutions on lines 8 through 12:
- Replace any leading space with nothing (otherwise you get an empty first column when the PID is right-aligned).
- Replace any trailing spaces with nothing (similarly to avoid empty fields after the data).
- Replace any remaining runs of adjacent spaces with a comma. Print this line.
Finally, on line 12, we are done, so we quit sed
.
The shell does not pay any attention to the name of the file you are redirecting into and generally, file extensions on Unix are informal decorations, not file type specifiers like they are on some platforms.
You could do echo hello >outputfile.ps
and the output would still be text, not PostScript (or a number of other possible interpretations of the .ps
file extension). In any event, the echo
command does not know that it is being redirected, because that is handled by the shell before the command runs, anyway. (Well, echo
is a shell built-in, so in theory there could be some coordination in this case.)
How to redirect output of top command in a file
top -b -n 1
seems to work on my Linux box here (-b: batch mode operation, -n: number of iterations).
Edit:
I just tried it on FreeBSD 9.2 which uses the 3.5beta12
version of top
. It seems it needs at least one additional iteration to get CPU stats. So you might want to use:
top -b -d2 -s1 | sed -e '1,/USERNAME/d' | sed -e '1,/^$/d'
-b: batch mode, -d2: 2 displays (the first one does not contain CPU stats, second one does), -s1: wait one seconds between displays
The sed
pipeline removes the first display which does not contain CPU stats (by skipping header and process list).
unable to capture top command output in a file using watch command
Managed to write the output of top in a file and add it in cron so that i can get the output in a file after every time interval>>
script:
!/bin/bash
top -b -n 5 -u mysql >> top.txt
crontab entry:
*/1 * * * * cd /scriptpath && sh script.sh
sample output:
top - 17:31:01 up 5 days, 5:59, 3 users, load average: 0.42, 0.38, 0.40
Tasks: 193 total, 1 running, 190 sleeping, 1 stopped, 1 zombie
Cpu(s): 19.6%us, 0.6%sy, 0.0%ni, 78.9%id, 0.3%wa, 0.1%hi, 0.5%si, 0.0%st
Mem: 24686168k total, 24549032k used, 137136k free, 663424k buffers
Swap: 12289716k total, 140k used, 12289576k free, 19292760k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3259 mysql 15 0 3967m 3.5g 6372 S 52.9 14.9 3382:39 mysqld
top - 17:31:04 up 5 days, 5:59, 3 users, load average: 0.47, 0.39, 0.41
Tasks: 192 total, 1 running, 189 sleeping, 1 stopped, 1 zombie
Cpu(s): 4.5%us, 2.5%sy, 0.0%ni, 89.7%id, 0.0%wa, 0.6%hi, 2.8%si, 0.0%st
Mem: 24686168k total, 24549520k used, 136648k free, 663424k buffers
Swap: 12289716k total, 140k used, 12289576k free, 19292772k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3259 mysql 15 0 3967m 3.5g 6372 S 64.1 14.9 3382:41 mysqld
top - 17:31:07 up 5 days, 5:59, 3 users, load average: 0.47, 0.39, 0.41
Tasks: 192 total, 1 running, 189 sleeping, 1 stopped, 1 zombie
Cpu(s): 4.0%us, 1.8%sy, 0.0%ni, 91.7%id, 0.0%wa, 0.5%hi, 2.0%si, 0.0%st
Mem: 24686168k total, 24549256k used, 136912k free, 663428k buffers
Swap: 12289716k total, 140k used, 12289576k free, 19292772k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3259 mysql 15 0 3967m 3.5g 6372 S 56.1 14.9 3382:43 mysqld
top - 17:31:10 up 5 days, 5:59, 3 users, load average: 0.43, 0.39, 0.40
Tasks: 192 total, 1 running, 189 sleeping, 1 stopped, 1 zombie
Cpu(s): 4.0%us, 2.3%sy, 0.0%ni, 90.7%id, 0.0%wa, 0.5%hi, 2.5%si, 0.0%st
Mem: 24686168k total, 24549876k used, 136292k free, 663428k buffers
Swap: 12289716k total, 140k used, 12289576k free, 19292772k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3259 mysql 15 0 3967m 3.5g 6372 S 56.8 14.9 3382:44 mysqld
top - 17:31:13 up 5 days, 5:59, 3 users, load average: 0.40, 0.38, 0.40
Tasks: 192 total, 1 running, 189 sleeping, 1 stopped, 1 zombie
Cpu(s): 4.0%us, 2.1%sy, 0.0%ni, 91.2%id, 0.0%wa, 0.5%hi, 2.2%si, 0.0%st
Mem: 24686168k total, 24549876k used, 136292k free, 663436k buffers
Swap: 12289716k total, 140k used, 12289576k free, 19292772k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3259 mysql 15 0 3967m 3.5g 6372 S 54.8 14.9 3382:46 mysqld
How to get output of top command
Use top in batch mode:
top -bn1 > output.txt
See also this previous answer.
How to correctly save the unix top command output into a variable?
Notice the difference:
#! /bin/bash
x=`top -b -n 1 | head -n 5`
echo $x
echo --------------------
echo "$x"
Output:
top - 14:33:09 up 7 days, 5:58, 4 users, load average: 0.00, 0.00, 0.09 Tasks: 253 total, 2 running, 251 sleeping, 0 stopped, 0 zombie Cpu(s): 1.6%us, 0.4%sy, 70.3%ni, 27.6%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 3926784k total, 3644624k used, 282160k free, 232696k buffers Swap: 9936160k total, 101156k used, 9835004k free, 1287352k cached
--------------------
top - 14:33:09 up 7 days, 5:58, 4 users, load average: 0.00, 0.00, 0.09
Tasks: 253 total, 2 running, 251 sleeping, 0 stopped, 0 zombie
Cpu(s): 1.6%us, 0.4%sy, 70.3%ni, 27.6%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 3926784k total, 3644624k used, 282160k free, 232696k buffers
Swap: 9936160k total, 101156k used, 9835004k free, 1287352k cached
Without the quotes, the contents of the variable are ground up in the shell's argument processing.
python script to capture output of top command
Since you're waiting for the process to finish, you need to tell top
to only print its output once, and then quit.
You can do that by running:
top -n 1
Getting the first 5 lines from PID and COMMAND using top
With your shown samples, please try following code. Since you need top 5 PIDs details so printing first 5 lines wouldn't work here. So skipping first 6 lines in output of top(which are about system details).
top -b | awk 'FNR>=7 && FNR<=12{print $1};FNR==12{exit}'
Explanation: Simple explanation of above code would be, passing top
command's output to awk
as an standard input. Then in awk
program checking condition if line number is from 7th to 12th then print it and on 12th line exit out of program.
Where definition of top -b
option is as follows from man top
:
b :Batch-mode operation Starts top in Batch mode, which could be
useful for sending output from top to other programs or to a file. In
this mode, top will not accept input and runs until the iterations
limit you've set with the `-n' command-line option or until killed.
Capture %CPU and PID of processes filtered by COMMAND using top command
Awk can do this; awk '/regex/ { print }'
performs the print
action only on lines matching regex
.
However, you can (and perhaps also should) subsume head
and tail
as well:
top -b -n 1 | awk 'NR>1 && $10 == "chrome" {print strftime("%T"), $1, $9}'
... assuming the tenth field of top
output contains the command name.
Related Topics
What Is the Command to Match Brackets in Emacs
What's the Meaning of a ! Before a Command in the Shell
How to Dynamically Allocate Memory Using Assembly and System Calls Under Linux
How to Improve Performance of Linux Pipes
Searching for a String in Multiple Files on Linux
Getting Memory Map of Every Device in Linux
Install Rabbitmqadmin on Linux
Why How to Cast Sockaddr to Sockaddr_In
Manage Source Under Git and Svn Simultanously - Does It Make Sense
How to List Library Dependencies of a Non-Native Binary
Is There Any Modern Review of Solutions to the 10000 Client/Sec Problem
How to Find the Tomcat 7 Installation Folder on Linux Ami in Elastic Beanstalk
Trace of Executed Programs Called by a Bash Script
How to Query X11 Display Resolution
Sort by Multiple Columns in Bash