Shay
Shay

Reputation: 693

get apache total cpu usage in (linux)

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?

Upvotes: 1

Views: 7509

Answers (3)

Viktor
Viktor

Reputation: 401

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}'

Upvotes: 1

newfurniturey
newfurniturey

Reputation: 38456

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.

Upvotes: 4

scai
scai

Reputation: 21519

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.

Upvotes: 1

Related Questions