Exporting top data to a file - linux

So I've been trying to run the following command to check the consumption by the firefox process.
top -b | grep "firefox"
Now I can see the desired output in terminal for it. But now when I try to export it to a file, I'm not able to.
The command I'm running is:
top -b | grep "firefox" >> filename or top -b | grep "firefox" > filename
Please help.

You need the -n parm for top. For example,
top -b -n 1 | grep "firefox" >> firefox.out
Without -n 1 top will keep processing and will never make it to grep..
From the man page for top:
-n :Number-of-iterations limit as: -n number
Specifies the maximum number of iterations, or frames, top
should produce before ending.
Updated code with a while loop. It will loop forever unless you use
something like cntr variable. Remove the cntr code if you want
continuous monitoring:
#!/bin/sh
#
not_done=1
cntr=0
# Look for process firefox every 1 seconds
while [ "$not_done" -eq 1 ]
do
top -b -n 1 | grep "firefox" >> /tmp/firefox.out
sleep 1
((cntr=cntr+1))
# Addition logic to break out of loop after 10 iterations
if [ "$cntr" -eq 10 ]
then
not_done=0
fi
echo "cntr=$cntr"
done

You have to add in the command the flag -n
Change the number of processes to show. You will be prompted to enter the number.
To take a snapshot of a specific process in top utility, execute command with the PID (-p) flag.
Also i recommed if you want to take snapshots for the process one (PID), and get taking three snapshots of the PID:
top -p 678 -b -n3 > monitoring.out

Related

How to get continuous logs of Linux top command to store

I am running a process process_a in loop .
I want to get top -H logs stored for all process_a running in loop.
top -b -H -p `pgrep -d, -f process_a`
the above command gave logs for process_a for first loop only
is there a way to get to get top logs for upcoming loops as well ?
This script will repeat forever. I added the -n 1 option. This allows you to rerun the pgrep for each iteration. Note: I used init for the name of the process. Change that to process_a for yours.
#!/bin/sh
while [ true ]; do
top -b -H -n 1 -p `pgrep -d, -f init`
sleep 1
done

Running a script with top command in the background

I have a script that basically prints that output of top -n1 to a file every second
In its simplest form:
while [ 1 ] ; do
top -n1
sleep 1
done
If I run my secript like:
./my_script.sh > out.log
it runs fine
If I run it in the background:
./my_script.sh > out.log &
Then it give me Stopped(SIGTTOU) error. From other Q/As I found that top is trying to read from the stdin, and when run in the background there is no stdin.
How can I achieve logging of top into a file as a background task?
You need to write top to file, and that in a loop..
#!/bin/bash
while [ 1 ] ; do
top -b -n 1 > top.txt
sleep 1
done
or
#!/bin/bash
while :
do
top -b -n 1 > top.txt
sleep 1
done

Collect stdout logs for a shell script that runs inside another shell script

I have a shell script called load_data.sh that runs inside a shell script called shell.sh
The contents of shell.sh
xargs --max-procs 10 -n 1 sh load_data.sh < tables.txt
This shell script runs on 10 tables at the same time in the tables.txt
Now I want to collect the Full logs of the load_data.sh So I did
xargs --max-procs 10 -n 1 sh load_data.sh < tables.txt |& tee-a logs.txt
But I am getting a mix of all the logs. What I want is the logs should be 1st table log then 2nd table log and then 3rd table logs and so on...
Is it possible to achieve that. If so how can I achieve that?
You can solve your problem by creating a separate logfile for each time your script is run. To get the logfiles to be created in sequence you can use the 'nl' utility to number each line of input.
nl -n rz tables.txt | xargs -n 2 --max-procs 10 sh -c './load_data.sh "$1" > log-$0'
Will produced logfiles in sequence
log-001
log-002
log-003
..
To turn that back into one file you can just use 'cat'
cat log* > result

bash execute command in variable

I have a command in a variable in Bash:
check_ifrunning=\`ps aux | grep "programmname" | grep -v "grep" | wc -l\`
The command checks if a specific program is running at the moment.
Later in my script, I want to query the value of the variable on a point.
If the specific program is running, the script should sleep for 15 minutes.
I solved it like this:
while [ $check_ifrunning -eq 1 ]; do
sleep 300
done
Will the script execute the command in the variable for each single loop-run or will the value in the variable stay after the first execution?
I have more variables in my script which can change their value. This was just one simple example of this.
Notice that check_ifrunning is set only once, in
check_ifrunning=`ps aux | grep "programmname" | grep -v "grep" | wc -l`
and that it is set before the loop:
while [ $check_ifrunning -eq 1 ]; do
sleep 300
done
You could add, for debugging purposes, an echo check_ifrunning is $check_ifrunning statement inside your while loop just before the sleep ...
You probably simply want (using pidof(8)) - without defining or using any check_ifrunning Bash variable:
while [ -n "$(pidof programname)" ]; do
sleep 300
done
Because you want to test if programname is running at every start of the loop!
You should use the more nestable and more readable $(...) instead of backquotes.
Consider reading the Advanced Bash Scripting Guide...
If you are writing a Bash script, consider to start it with
#!/bin/bash -vx
while debugging. When you are satisfied, remove the -vx...
If you want to encapsulate your commands, the proper way to do that is a function.
running () {
ps aux | grep "$1" | grep -q -v grep
}
With grep -q you get the result as the exit code, not as output; you use it simply like
if running "$programname"; then
:
Ideally, the second grep is unnecessary, but I did not want to complicate the code too much. It still won't work correctly if you are looking for grep. The proper solution is pidof.
See also http://mywiki.wooledge.org/BashFAQ/050

Filter by process name and log CPU usage

Is there an option for the Linux top command where I can filter processes by name and write the CPU usage of that process every second to a log file?
top & pgrep
To filter the output of top by process name, you can use pgrep to get a list of PIDs by process name then pass them to the -p option of top.
For example:
top -p $(pgrep -d',' http)
Note: the -d',' option delimits the PIDs with commas, which is what is expected by the top -p.
Note 2: top will return a failure message if there are no running processes that match the name you specify in pgrep.
To write the results of top to a file, use the -n 1 option (only one iteration) and redirect the output to your log file.
top -p $(pgrep -d',' http) -n 1 >> your_log_file
To do that every second, perhaps a while loop with a sleep would do?
while :; do top -p $(pgrep -d',' http) -n 1 >> your_log_file; sleep 1; done
To timestamp each entry, you can append the output of date. E.g.
while :; do top -p $(pgrep -d',' http) -n 1 >> log.txt; date >> log.txt; sleep 1; done
Another option is:
top -b -d 1 -p $(pgrep -d',' java) -n 120 > log.txt
The option -d allows to set the frequency used by top to refresh the
data.
The option -b means that the traditional interface of top is
not used. Instead, it sends everything to the standard output and then you can use a pipe (|) or a redirection (>).
The option -n informs about the number of iterations top should execute.
After that you can type:
cat log.txt | grep USER_OF_PROCESS
You will see the execution time of the process and also %CPU, Memory and all that.
#You can run following script as ./cpurecorder.sh pid filename
#It will generate output file with memory usage and cpu utilisation.
#You can log other variable by searching man for ps.
`enter code here`filepath=/home/rtcsadm # modify as desired
interval=20 # reports per minute
timelimit=6000 # how long to run, in seconds
mydate=`date "+%H:%M:%S"` # the timestamp
freq=$((60/$interval)) # for sleep function
while [ "$SECONDS" -le "$timelimit" ] ; do
ps -p$1 -opid -opcpu -opmem -ocomm -c | grep $1 | sed "s/^/$mydate /" >> $filepath/$2.txt
sleep 3
mydate=`date "+%H:%M:%S"`
done
By using the linux command "top"...
Then press the 'o' or 'O' to activate filter prompt. It will show a line indicating the filter format like this -
add filter #1 (ignoring case) as: [!]FLD?VAL
Then enter a filter like this and hit Enter.
COMMAND=<pattern>
Now top will show only those processes whose COMMAND field contains the "<pattern>" value
(Solution taken from ... https://techantidote.com/filter-top-using-process-name-in-linux/ )

Resources