linuxbashloadcronresource-utilization

Get the load, cpu usage and time of executing a bash script


I have a bash script that I plan to run every 5 or 15 mins using crontab based on the load it puts on server.

I can find time of running the script, but load, memory usage and CPU usage I am not sure how to find.

Can someone help me?

Also any suggestions of rough benchmark that will help me decide if the script puts too much load and should be run every 15 mins and not 5 mins.

Thanks in Advance!


Solution

  • You can use "top -b", top gives the CPU usage, memory usage etc, Insert these lines in your script, this will process in background and will terminate the process as soon as your testing overs.

     ssh server_name "nohup top -b -d 0.5 >> file_name &"   
    

    \top process will run in background because of &, -d 0.5 will give you the cpu status at every 0.5 secs, redirect the output in file_name for latter analysis.

    for killing the process after your test, insert following in your script,

    ssh server_name "kill \`ps -elf | grep 'top -b' | grep -v grep | sed 's/  */ /g' |cut -d ' ' -f4\`"
    

    Your main testing script should be between top command and command for killing top.

    I presumed you are running the script from client side, if not ignore "ssh server_name".

    If you are running it from client side, because of "ssh", you will be asked for the password, for avoiding this follow these 3 simple steps

    This will definitely solve the issue.