Get Linux top command result - linux

I want to get top command result of a specific process and then save it in a file.
I tried the following command:
top | grep "qemu" > file
but this doesn't work.
How can I implement this?

Use the -b and -n command line options:
top -bn1 | grep qemu > file
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' com‐
mand-line option or until killed.
-n :Number-of-iterations limit as: -n number
Specifies the maximum number of iterations, or frames, top should produce before ending.

Try this :
top -b -p `pgrep -d, -f qemu` > top-output.txt
-b : Batch mode operation
Starts top in "Batch mode", which could be useful for sending out-
put 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.

Related

How to stop writing to a capture file using tcpdump after it reaches a specific size

I am looking for some solution to stop capturing the tcpdump packet after it capture a specified size .I am using the below command to achieve this but it looks like the tcpdump is not writing all the captured packet to the specified file(myfile.pcap).
sudo tcpdump -i en0 -C 10 -W 1 -z ./stop-tcpdump.sh -w myfile.pcap -K -n
cat stop-tcpdump.sh
#!/bin/sh
TCP_EXECUTABLE="tcpdump"
pid=$(pidof ${TCP_EXECUTABLE})
sudo kill -2 $pid
The easiest solution for tcpdump is probably just to increase -W 1 to -W 2. This will cause a 2nd capture file to begin to be written, but the 1st file of 10MB will remain fully intact instead of getting truncated, because the tcpdump instance won't necessarily be killed due to timing issues before that happens.
Alternatively, you could switch to using dumpcap or tshark, both of which support an explicit -a filesize:value option, so no post-rotate kill script is needed. Note that unlike tcpdump's -C option, this option expects the value in units of kB, not MB.

How to display as a table combined output of a program and a file and watch it in 1 sec

I have a program isig, that displays 18 rows of data, want to combine it with text file info1.txt that also has 18 rows of additional data into a table view and watch it in 1sec interval.
My command to display the table is:
pr -m -t <(isig 4001+18) info1.txt
And if I add watch:
watch -n 1 'pr -m -t <(isig 4001+18) info1.txt'
I get:
sh: 1: Syntax error: "(" unexpected
Is there a way to achieve this in bash command line
Process substitution (<()) is a Bash extension, not available in standard shell. Get watch to run your command in Bash instead:
watch -n 1 'bash -c "pr -m -t <(isig 4001+18) info1.txt"'

How to output state into multiple text in script of Linux?

I have multiple servers of Linux, where I need to test the performance of my program, here I want to output the system state when running programs. In script of linux, I use following to output:
top -b -d 5 > System.txt
iostat -d 8 > IO.txt
But unfortunately, only the system.txt can be produced, but there is no the IO.txt file, so that need to add some thing in script to make these two file exist together?
Use iostat command like this :-
iostat -d 8 1 > IO.txt.
In this 1 means the limit, how many time do you want to run the command get the result.

Shell script does not work when run as daemon

I got a simple script (to measure CPU overhead)
#!/bin/bash
WAIT=2
i=1
while :
do
# Obtain the cpu usage
top -n 1 > t.$i
i=$(($i+1))
sleep $WAIT
done
When I run it as
./Script.sh
It works as expected.
But when I run it as
./Script.sh&
it does nothing (files are not created). What am I missing here?
Change the line where you invoke top to be:
top -b -n 1 > t.$i
This enables batch mode - good call Karoly

top not dumping correct cpu usage

I have a program that parses file dumps generated by dumping top into a text file. For example, I use top -n 1 -b > dump1. The problem is when my system is under load for example 60% cpu utilization top always returns 3-4% usage of cpu. When I run top manually, the cpu starts at 3-4% usage, then after 1-2 seconds it will jump to the expected load. The question is how do I capture top a couple of seconds after it has been executed?
Also you can try bash script which only capture cpu usage and put in log file like
#!/bin/bash
while :; do
top -bn 1 | sed -n '3p' >> log.txt
sleep 2
done
Here top command fire every 2 seconds.

Resources