首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >按进程名称过滤并记录CPU使用情况

按进程名称过滤并记录CPU使用情况
EN

Stack Overflow用户
提问于 2012-01-03 18:07:43
回答 1查看 62.3K关注 0票数 71

linux top命令中是否有一个选项可以让我按名称过滤进程,并每隔1秒将该进程的CPU使用情况写入日志文件?

EN

回答 1

Stack Overflow用户

发布于 2014-01-14 19:28:21

代码语言:javascript
复制
#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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8710584

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档