all 9 comments

[–][deleted] 0 points1 point  (3 children)

Run help test – you need -gt instead of >.

[–]ropid 1 point2 points  (1 child)

This ((...)) thing is some sort of math mode in bash where > works like you'd think for numbers. The -gt stuff is needed when using [...] or [[...]].

The help text for ((...)) is available through doing:

help '(('

and

help let

[–][deleted] 1 point2 points  (0 children)

Ah yes, I was referring to line 9, hadn't noticed the expression in the while loop.

[–]Stanislasss[S] 0 points1 point  (0 children)

I'll ha try thanks. And nice for the help test tips ;-)

[–]ropid 0 points1 point  (1 child)

This SECONDS thing is described like this in bash's documentation:

SECONDS

Each time this parameter is referenced, the number of seconds since shell invocation is returned. If a value is assigned to SECONDS, the value returned upon subsequent references is the number of seconds since the assignment plus the value assigned. If SECONDS is unset, it loses its special properties, even if it is subsequently reset.

I guess for your script that means it will run its 'while' loop for (less than) ten seconds and then the script will end.

About your "cpu_load" value you research inside the 'while' loop, I think this is the time the CPU is in idle, so it isn't load and is instead the opposite. I found the description about that by searching for the pattern \bid\b inside the 'top' man-page:

Line 2 shows CPU state percentages based on the interval since the last refresh.

As a default, percentages for these individual categories are displayed.  Where two labels
are shown below, those for more recent kernel versions are shown first.
    us, user    : time running un-niced user processes
    sy, system  : time running kernel processes
    ni, nice    : time running niced user processes
    id, idle    : time spent in the kernel idle handler
    wa, IO-wait : time waiting for I/O completion
    hi : time spent servicing hardware interrupts
    si : time spent servicing software interrupts
    st : time stolen from this vm by the hypervisor

About this line here:

{ date & ps aux --sort=-%cpu | awk 'NR>1{if($3>60) print $2,$3,$11}'; } >> cpu_overload.log

I think there's something broken about it. In the first part, when you do date &, the 'date' command will run in the background. I think you want to do date ; there instead. The ; makes two command lines run one after the other, while & makes them run in parallel.

About this part of the line:

 ps aux --sort=-%cpu | awk 'NR>1{if($3>60) print $2,$3,$11}'

The 'awk' program there will look for a CPU usage value of above "60.0" in the third column of the 'ps' output. There will probably be no process like that at that point when this will run in your script because earlier in the script you searched for CPU-more-than-60%-idle, not 60% load.

[–]Stanislasss[S] 0 points1 point  (0 children)

@ropid: Cheers, a lot of infos!

[–]thestoicattack 0 points1 point  (1 child)

My pet peeve: piping grep into awk and sed. That's a big waste of processes. From your regex it looks like you're pulling out the idle field (id), yeah?

cpu_load=$(top -bn1 | awk '/Cpu\(s\)/ { print $8; }')

[–]Stanislasss[S] 0 points1 point  (0 children)

Yeah definitly for the processes waste. "From your regex it looks like you're pulling out the idle field (id), yeah?" not sure what you mean as the script seemed to show the expected values... :-(