Linux 按进程名称过滤并记录 CPU 使用率

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8710584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:53:37  来源:igfitidea点击:

Filter by process name and log CPU usage

linuxunix

提问by BalaB

Is there an option for linux top command where i can filter processes by name and write the CPU usage of that process every 1 second to a log file?

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

采纳答案by Shawn Chin

top& pgrep

top& pgrep

To filter the output of topby process name, you can use pgrepto get a list of PIDs by process name then pass them to the -poption of top.

要按top进程名称过滤输出,您可以使用按进程名称 pgrep获取PID列表,然后将它们传递给 的-p选项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: topwill return a failure message if there are no running processes that match the name you specify in pgrep.

注意:该-d','选项用逗号分隔 PID,这是top -p. 注 2:top如果没有与您指定的名称匹配的正在运行的进程,将返回失败消息pgrep

To write the results of topto a file, use the -n 1option (only one iteration) and redirect the output to your log file.

要将结果写入top文件,请使用该-n 1选项(仅一次迭代)并将输出重定向到您的日志文件。

top -p $(pgrep -d',' http) -n 1 >> your_log_file

To do that every second, perhaps a whileloop with a sleepwould do?

每秒都这样做,也许一个while带有 a的循环sleep会做?

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.

要为每个条目添加时间戳,您可以附加date. 例如

while :; do top -p $(pgrep -d',' http) -n 1 >> log.txt; date >> log.txt; sleep 1; done

回答by Inti Gonzalez-Herrera

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.
  • 选项 -d 允许设置 top 用于刷新数据的频率。
  • 选项 -b 表示不使用传统的 top 接口。相反,它将所有内容发送到标准输出,然后您可以使用管道 (|) 或重定向 (>)。
  • 选项 -n 通知 top 应该执行的迭代次数。

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.

您将看到进程的执行时间以及 %CPU、内存等等。

回答by user3193783

#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 -opid -opcpu -opmem -ocomm -c | grep  | sed "s/^/$mydate /" >> $filepath/.txt
  sleep 3
  mydate=`date "+%H:%M:%S"`
done