Saturday, April 26, 2014

Shell script to record linux top to a text file continuously after a given time

We can record the whole top or a specific process with a process ID or process name. You can customize this code with existing shell commands related to linux shell command options relate with top "top".  Refer to http://linux.about.com/od/commands/l/blcmdl1_top.htm

Replace
[1] with the name of the process.
[2] with the name of the text file where the output is need to be recorded.

while [ 1 ] 

     do top -n 1 -p $(pidof firefox[1]) -b >> topoutput.txt[2]

     sleep 0.1 

done 


If you need to track the process just with the process id use below script.

Replace
[3] with the id of the process.

while [ 1 ] 

     do top -n 1 -p 2304[3] -b >> topoutput.txt[2]

     sleep 0.1 

done 

Write above codes with your favourite text editor and save it with .sh

To start this batch process via shell type

bash <file name of the shell script>.sh

No comments:

Post a Comment