get apache total cpu usage in (linux)
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
.
Linux : Get total cpu usage by httpd
try this in ssh
ps aux | grep "httpd" | awk '{sum1 +=$3}; END {print sum1}'
output is:
10.5
and this for sum of memory
ps aux | grep "httpd" | awk '{sum1 +=$4}; END {print sum1}'
Retrieve CPU usage and memory usage of a single process on Linux?
ps -p <pid> -o %cpu,%mem,cmd
(You can leave off "cmd" but that might be helpful in debugging).
Note that this gives average CPU usage of the process over the time it has been running.
Efficient way to calculate cpu usage of multiple processes in linux
You should do it using /proc
as you have already discovered. Or perhaps using some higher-level library if your language of choice offers one. Parsing the output of ps
and top
is not winning you anything. The data you need are all in /proc
, you just need to be careful to check the identity of each process over time, in case PIDs get recycled while you are collecting data (on most systems PID recycling won't happen in the span of mere seconds, so it's workable).
Apache2 periodically using 100% CPU
Ok, so in the end I was able to track down the problem.
Some crawlers for popular SEO tools were requesting non-existent URLs at a very high frequency, resulting in some processes being triggered within Pimcore. That's what was driving up CPU and RAM consumption.
After blocking those crawlers via .htaccess, everything is back to normal.
Accurate calculation of CPU usage given in percentage in Linux?
According the htop source code, my assumptions looks like they are valid:
(see static inline double LinuxProcessList_scanCPUTime(LinuxProcessList* this)
function at LinuxProcessList.c)
// Guest time is already accounted in usertime
usertime = usertime - guest; # As you see here, it subtracts guest from user time
nicetime = nicetime - guestnice; # and guest_nice from nice time
// Fields existing on kernels >= 2.6
// (and RHEL's patched kernel 2.4...)
unsigned long long int idlealltime = idletime + ioWait; # ioWait is added in the idleTime
unsigned long long int systemalltime = systemtime + irq + softIrq;
unsigned long long int virtalltime = guest + guestnice;
unsigned long long int totaltime = usertime + nicetime + systemalltime + idlealltime + steal + virtalltime;
And so, from fields listed in the first line of /proc/stat
: (see section 1.8 at documentation)
user nice system idle iowait irq softirq steal guest guest_nice
cpu 74608 2520 24433 1117073 6176 4054 0 0 0 0
Algorithmically, we can calculate the CPU usage percentage like:
PrevIdle = previdle + previowait
Idle = idle + iowait
PrevNonIdle = prevuser + prevnice + prevsystem + previrq + prevsoftirq + prevsteal
NonIdle = user + nice + system + irq + softirq + steal
PrevTotal = PrevIdle + PrevNonIdle
Total = Idle + NonIdle
# differentiate: actual value minus the previous one
totald = Total - PrevTotal
idled = Idle - PrevIdle
CPU_Percentage = (totald - idled)/totald
Related Topics
Run Meteor as a Daemon Process
Use Awk to Print $0 Using The Same Format for All Columns
Link Extraction from a Google Page in Bash
Why This Shell Won't Work If It's Called from Rc.Local But Ssh
Awk: Sum Up Column Values Across Multiple Files with Identical Column Layout
Cannot Find Module 'Firebase-Admin' When Trying to Deploy Firebase Functions
Container Running in Privileged Mode
Code for Wait_Event_Interruptible
Why Does Cat <<< $Var1 Lose Newlines
How to Make Sudo Save The Password
Search Ip from a Text File in .Csv Log File, If Found Add New Column Next to It
Different Pulic Key for Jump Host and Destination Host Over Ssh
How to Find Libstdc++.So.6: That Contain Glibcxx_3.4.19 for Rhel 6