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

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 ?

Related

How to display processes that are using memory in an given range

How can I display processes that are using memory between an given interval in terminal? For exemple: processes that are using between 50 and 100 MB of Memory.
I tried:
ps aux | awk '{print $4}' | sort
but this only displays the memory for every process sorted and not in an interval.
This will list processes as expected. Remember that ps shows memory size in kilobytes.
ps -u 1000 -o pid,user,stime,rss \
| awk '{if($4 > 50000 && $4 < 100000){ print $0 }}' \
| sort -n -k 4,4
Command output:
3407 luis.mu+ 10:30 51824
3523 luis.mu+ 10:30 66108
3410 luis.mu+ 10:30 71060
3595 luis.mu+ 10:30 74340
3609 luis.mu+ 10:30 77772
18550 luis.mu+ 16:47 93616
In that case it's showing only 4 fields for user id 1000. To show all processes use
ps -e -o pid,user,stime,rss
From the ps(3) man page under STANDARD FORMAT SPECIFIERS:
rss
resident set size, the non-swapped physical memory that a task has used (inkiloBytes)
If you want to show more fields, check the man page and add fields to -o option.
For more complex testing, including comparison, inequality, and numerical tests, awk is very useful:
ps aux | awk '{print $4}' | sort | awk '$1 >= 1 && $1 <=2'| cat
Here I am checking the memory usage between 1MB and 2MB using awk and printing them using cat.

Get CPU and Memory Info In Different HPUX unix Servers

I am trying to make a bash script to get CPU and Memory information in different Unix servers and pipe them into a single text file in the local server.
The script returns something (result), but the result was not accurate when I check each of the servers CPU and Memory information manually. There might be some error in the scripts since the CPU and Memory commands are correct.
Here is the command to get CPU information in Unix (HPUX) :
machinfo | grep CPUs | awk '{print $5}'
And the result will be :
4
And here is the command to get Memory information in Unix (HPUX) :
machinfo | grep Memory | awk '{print $3}'
And the result will be :
4090
It shows that the commands are correct. Let's go into the full scripts.
#!/bin/ksh
SERVERS='
xxx.xxx.xxx.xxx
xxx.xxx.xxx.xxx'
cpu=$(machinfo | grep CPUs | awk '{print $5}')
mem=$(machinfo | grep Memory | awk '{print $3}')
print "SERVER" "CPU" "RAM (MB)";
for i in $SERVERS
do
ssh -n $i 2>&1 '$cpu | $mem'
print $cpu $mem
done
When the full scripts run, the result will be:
SERVER CPU RAM (MB)
s24bf011 2 4090
s24bf012 2 4090
s24bf013 2 4090
s24bf014 2 4090
s24bf016 2 4090
S24BF104 2 4090
S24BF105 2 4090
10.14.24.158 2 4090
S29BF200 2 4090
S02BF131 2 4090
S02BF104 2 4090
S24BF071 2 4090
S24BF165 2 4090
10.14.24.17 2 4090
Every server has a different CPU and RAM. How come they were all the same?
Error in SSHing?
Your script has multiple problems.
'$cpu | $mem'
is just a literal string. If you want to interpolate the values of $cpu and $mem, you need to use double quotes instead of single.
But
"$cpu | $mem"
will basically discard the output from $cpu because you are piping it to $mem which is ignoring its standard input. The proper way to run two independent commands is to put a semicolon between them, not a pipe. The function of the pipe is to take the standard output from the command before the pipe and redirect it as standard input into the command after the pipe.
But of course, the variables you declared do not contain the scripts; they contain the output of the Awk scripts, which you ran locally when you put them in process substitutions.
You should have received error messages about all of this; what happened to them?
Anyway, you can simplify processing enormously by refactoring this to just run a single command, anyway.
print "SERVER" "CPU" "RAM (MB)";
for i in $SERVERS
do
ssh -n "$i" "machinfo |
awk -v s='$i' '/CPUs/ { c=\$5 } /Memory/ { m=\$3 }
END { print s, c, m }'"
done
Here, I use double quotes around the remote command so that I can interpolate the server name into the Awk variable s. The internal single quotes inside the double quotes do not have any significance to the local shell, i.e. they do not prevent the local shell from replacing the dollar signs, so we need to protect the dollar signs by backslash-escaping them.
But unless your local Awk is crippled or something, it doesn't make sense to run Awk remotely.
for i in $SERVERS
do
ssh -n "$i" machinfo |
awk -v s="$i" '/CPUs/ { c=$5 } /Memory/ { m=$3 }
END { print s, c, m }'
done
Putting stuff which isn't variable and which is only used once into a variable is just silly, and sometimes problematic (see http://mywiki.wooledge.org/BashFAQ/050).

Running time in secs for a process in linux

I am trying to find the running time of a process on a linux server. I using etime to achieve this. Here's the code i am using
ps -eo etime,args | grep "process"|awk '{print $1}'
The sample output for this
28-08:42:13
I am then doing the following to convert this to seconds and sending myself a mail when the runtime is less that 30 mins(1800 secs)
ps -eo etime,args | grep "process"|awk '{print $1}'|awk -F'[-: ]+' '/:/ {t=$2+60*$1; print t}'
The problem i am seeing is, when converting to seconds, it's taking the $2(secs) and $1(mins) from the running time and sending me a mail even though the process has been up for 28-08:42:13.
Is there a better way of doing the conversion of runtime to secs? Using $3 and $4 from the first output doesn't work for cases where the runtime is less than 1 hour. It's picking a garbage value and returning a different value than the actual running time.
Please help.
etime is human readable elapsed time. etimes is machine readable elapsed time (in seconds):
$ ps -o etimes,etime,cmd $$
ELAPSED ELAPSED CMD
515615 5-23:13:35 bash
I am using this and it works.
ps -eo etime,args | grep "process"|awk '{print $1}'|awk -F'[-: ]+' '/:/ && NF==5 {t=$5+60*($4+60*($3+24*($2+365*$1))); print t} /:/ && NF==4 {t=$4+60*($3+60*($2+24*$1)); print t} /:/ && NF==3 {t=$3+60*($2+60*$1); print t} /:/ && NF==2 {t=$2+60*$1; print t}'

Calculate percentage free swap space with `free` and `awk`

I'm trying to calculate the percentage of free swap space available.
Using something like this:
free | grep 'Swap' | awk '{t = $2; f = $4; print ($f/$t)}'
but awk is throwing:
awk: program limit exceeded: maximum number of fields size=32767
And I don't really understand why, my program is quite simple, is it possible I'm having a weird range error?
Try this one :
free | grep 'Swap' | awk '{t = $2; f = $4; print (f/t)}'
In your code you are trying to print $f and $t which is respectively $FreeMemory and $TotalMemory. So i guess you have about 4gig ram in total which would refer to ~ $400000 which is a little bit over the total of fields awk uses in standard config. Apart from the easier attempt with meminfo try just printing f/t which refers to the variables and you get your answer.
Note that it might be easier/more robust to read the info by using /proc/meminfo's SwapFree line.
Something like:
$ grep SwapFree /proc/meminfo | awk '{print $2}'
You do not need the variables. You can use plain
awk '{ print $4/$2 }'
Read it from /proc/meminfo:
lennart#trololol:~$ grep SwapFree /proc/meminfo | awk '{print $2}'
0
I realise that the question is about using "free" and "awk", but if you have SAR running, then this will give you the most recently recorded percentage value:
sar -S|tail -2|head -1|awk '{print $5}'

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?
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.

Resources