get apache total cpu usage in (linux) - 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?

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.

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

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.

Related

Ps command shows more than 200% load, on dualcore cpu

I use ps -aux | awk 'NR > 1 {sum += $3;} END {print sum;}' on dualcore cpu.
But when i turning on a stress test, i sometimes have peak about 230-250% for few seconds. After it slowly get around 190-196%
What i do wrong ?

Where to look at the highest cpu usage in linux

I know about VmHWM usage in linux in /proc/[pid]/status where I can look at high water mark. Is there something similar for cpu usage? I mean top -p [pid] shows the current cpu usage. But I would like to know about the highest... Is there something for that in linux
You can use top -o cpu -n 1 | grep 'PID' -A1.
The command does the following:
top -o cpu returns sorts by cpu usage
-n 1 shows only the first line
grep 'PID' -A1 greps for PID and returns the following line
This will give you a constantly updating output of two lines. The first one are the labels and the second one the process.
Depending on what you want to use it for you can do some string processing with awk or sed

How to trace the list of PIDs running on a specific core?

I'm trying to run a program on a dedicated core in Linux. (I know Jailhouse is a good way to do so, but I have to use off-the-shelf Linux. :-( )
Other processes, such as interrupt handlers, kernel threads, service progresses, may also run on the dedicated core occasionally. I want to disable as many such processes as possible. To do that, I need first pin down the list of processes that may run on the dedicated core.
My question is:
Is there any existing tools that I can use to trace the list of PIDs or processes that run on a specific core over a time interval?
Thank you very much for your time and help in this question!
TL;DR Dirty hacky solution.
DISCLAIMER: At some point stops working "column: line too long" :-/
Copy this to: core-pids.sh
#!/bin/bash
TARGET_CPU=0
touch lastPIDs
touch CPU_PIDs
while true; do
ps ax -o cpuid,pid | tail -n +2 | sort | xargs -n 2 | grep -E "^$TARGET_CPU" | awk '{print $2}' > lastPIDs
for i in {1..100}; do printf "#\n" >> lastPIDs; done
cp CPU_PIDs aux
paste lastPIDs aux > CPU_PIDs
column -t CPU_PIDs > CPU_PIDs.humanfriendly.tsv
sleep 1
done
Then
chmod +x core-pids.sh
./core-pids.sh
Then open CPU_PIDs.humanfriendly.tsv with your favorite editor, and ¡inspect!
The key is in the "ps -o cpuid,pid" bit, for more detailed info, please comment. :D
Explanation
Infinite loop with
ps -o cpuid,pid | tail -n +2 | sort | xargs -n 2 | grep -E "^$TARGET_CPU" | awk '{print $2}' > lastPIDs
ps ax -o cpuid,pid
Show pid's associated to CPU
tail -n +2
remove headers
sort
sort by cpuid
xargs -n 2
remove white spaces at begging
grep -E "^$TARGET_CPU"
filter by CPU id
awk '{print $2}'
get pid column
> lastPIDs
output to file those las pid's for the target CPU id
for i in {1..10}; do printf "#\n" >> lastPIDs; done
hack for pretty .tsv print with the "columns -t" command
cp CPU_PIDs aux
CPU_PIDs holds the whole timeline, we copy it to aux file to allow the next command to use it as input and output
paste lastPIDs aux > CPU_PIDs
Append lastPIDs columns to the whole timeline file CPU_PIDs
column -t CPU_PIDs > CPU_PIDs.humanfriendly.tsv
pretty print whole timeline CPU_PIDs file
Attribution
stackoverflow answer to: ps utility in linux (procps), how to check which CPU is used
by Mikel
stackoverflow answer to: Echo newline in Bash prints literal \n
by sth
stackoverflow answer to: shell variable in a grep regex
by David W.
superuser answer to: Aligning columns in output from a UNIX command
Janne Pikkarainen
nixCraft article: HowTo: Unix For Loop 1 to 100 Numbers
The best way to obtain what you want is to operate as follows:
Use the isolcpus= Linux kernel boot parameter to "free" one core from the Linux scheduler
Disable the irqbalance daemon (in case it is executing)
Set the IRQs affinities to the other cores by manually writing the CPU mask on /proc/irq/<irq_number>/smp_affinity
Finally, run your program setting the affinity to the dedicated core through the taskset command.
In this case, such core will only execute your program. For checking, you can type ps -eLF and look at the PSR column (which specifies the CPU number).
Not a direct answer to the question, but I am usually using perf context-switches software event to identify the perturbation of the system or other processes on my benchmarks

Get pid of last started instance of a certain process

I have several instances of a certain process running and I want to determine the process id of the one that has been started last.
So far I came to this code:
ps -aef | grep myProcess | grep -v grep | awk -F" " '{print $2}' |
while read line; do
echo $line
done
This gets me all process ids of myProcess. Somehow I need to compare now the running times of this pids and find out the one with the smallest running time. But I don't know how to do that...
An easier way would be to use pgrep with its -n, --newest switch.
Select only the newest (most recently started) of the matching
processes.
Alternatively, if you don't want to use pgrep, you can use ps and sort by start time:
ps -ef kbsdstart
Use pgrep. It has a -n (newest) option for that. So just try
pgrep -n myProcess

linux cpu usage

I am working on unix.
I want to knwo the current cpu usage of a process.
I understood that ps give the average of cpu used till the process is up - it is not the current usage.
Is there a way to print only the cpu from the top command without 10 more parameters and
headers? I know how to do it with awk - this is not the way i want to do it.
top -p 20705 -bc -n 1 | tail -n 2 | awk '{ print $9}' | head -n 1
If there is another simple way to do it, not reading /proc/stat...
If there is a simple way doing it from c++, it is also ok.
Most likely, you will need to read /proc/stat, However, here is an interesting article with C code that may help you out. To understand and use the output from the program you should do man 5 proc. And here is the source code.
The bottom line is that you will need to read from /proc/stat to do what you want.
to see cpu usage of a proccess whose pid is 24556
ps -p 24556 -o \%cpu=
to see mem usage of a process named syslogd
ps -C syslogd -o \%mem=

Resources