获取执行bash脚本的负载,CPU使用率和时间 [英] Get the load, cpu usage and time of executing a bash script

查看:128
本文介绍了获取执行bash脚本的负载,CPU使用率和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个bash脚本,我计划根据它在服务器上施加的负载使用crontab每5或15分钟运行一次.

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.

我可以找到运行脚本的时间,但是我不确定如何找到负载,内存使用量和CPU使用率.

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

有人可以帮助我吗?

还有一些粗略基准测试的建议,这些建议可以帮助我确定脚本是否负担过多,应该每15分钟而不是5分钟运行一次.

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.

预先感谢!

推荐答案

您可以使用"top -b",top给出CPU使用率,内存使用率等信息, 将这些行插入脚本中,这将在后台处理,并在测试结束后立即终止该过程.

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进程将因为& amp;在后台运行,-d 0.5将每0.5秒为您提供cpu状态,将输出重定向到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\`"

您的主要测试脚本应位于top命令和杀死top命令之间.

如果不忽略"ssh server_name",我想您是从客户端运行脚本的.

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

如果由于"ssh"而从客户端运行该密码,系统将要求您输入密码,以免

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

这肯定会解决问题.

这篇关于获取执行bash脚本的负载,CPU使用率和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆