get apache total cpu usage in (linux)

回眸只為那壹抹淺笑 提交于 2019-12-08 06:24:00

问题


I want to write a script (in bash or Perl on linux) which monitors the Apache and restarts the Apache in case it exceeds X% CPU. I understand that I need to get the total CPU usage of Apache since it opens child process.

How can I get the total CPU usage of Apache?


回答1:


Try the following, but make sure to update the Apache-process name with your actual one (mine is httpd):

ps u -C httpd | awk '{sum += $3} END {print sum}'

This will get a list of all apache processes running and sum the %CPU column of ps's output using awk.




回答2:


this will return sum load of parent apache process and all child processes, in percents, without any additional info, so that you can easily use this script in any way:

ps --no-heading -o pcpu -C httpd | awk '{s+=$1} END {print s}'



回答3:


This will list you the total CPU usage of each apache2 process:

ps u -C apache2 | awk '{print $3}' | grep -v "%CPU"

Note, however, that the total (=average) CPU usage might be rather low even if the current CPU usage is high, especially for long running processes.



来源:https://stackoverflow.com/questions/11935910/get-apache-total-cpu-usage-in-linux

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!