问题
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