Page 1 of 1

Bash script question...

Posted: Tue Sep 25, 2018 5:46 pm
by invex
What command would I need to:
pause until CPU usage less than x%

TIA

Re: Bash script question...

Posted: Wed Sep 26, 2018 3:37 am
by administrollaattori

Code: Select all

cpulimit

Re: Bash script question...

Posted: Wed Sep 26, 2018 6:21 am
by rene
Install the sysstat package and try

Code: Select all

LC_NUMERIC=C mpstat 1 | awk -v x=1 'NR > 3 { if ($12 > 100-x) exit }'
in which of course the "1" in "x=1" is to be replaced.

You may have gotten a notification a bit earlier when I posted this without the LC_NUMERIC=C: shortly after posting I noticed it not in fact working and removed the answer while finding some time to see why not. Turned out to be a locale issue: decimal point (US, UK, ...) versus comma (most of the rest of the world), fixed either by telling awk to adapt by giving it --use-lc-numeric or as per above telling mpstat to not. Gory details over at https://www.gnu.org/software/gawk/manua ... onversions.

This question is by the way rather misplaced here in Desktop & Window Managers -> Cinnamon; should be in the Scripts & Bash section. Am I mistaken that it in fact was there when I posted earlier? :?

Re: Bash script question...

Posted: Wed Sep 26, 2018 6:32 am
by Moem
Mod note:
Topic moved to Scripts & Bash.

Re: Bash script question...

Posted: Wed Sep 26, 2018 12:13 pm
by invex
rene wrote:
Wed Sep 26, 2018 6:21 am
Install the sysstat package and try

Code: Select all

LC_NUMERIC=C mpstat 1 | awk -v x=1 'NR > 3 { if ($12 > 100-x) exit }'
in which of course the "1" in "x=1" is to be replaced.

You may have gotten a notification a bit earlier when I posted this without the LC_NUMERIC=C: shortly after posting I noticed it not in fact working and removed the answer while finding some time to see why not. Turned out to be a locale issue: decimal point (US, UK, ...) versus comma (most of the rest of the world), fixed either by telling awk to adapt by giving it --use-lc-numeric or as per above telling mpstat to not. Gory details over at https://www.gnu.org/software/gawk/manua ... onversions.

This question is by the way rather misplaced here in Desktop & Window Managers -> Cinnamon; should be in the Scripts & Bash section. Am I mistaken that it in fact was there when I posted earlier? :?
Thank you very much.
Maybe I'm doing something wrong but the following command never exits (should exit if CPU is under 50%):
LC_NUMERIC=C mpstat 1 | awk -v x=50 'NR > 3 { if ($12 > 100-x) exit }'

Re: Bash script question...

Posted: Wed Sep 26, 2018 1:24 pm
by rene
invex wrote:
Wed Sep 26, 2018 12:13 pm
Maybe I'm doing something wrong but the following command never exits (should exit if CPU is under 50%):
Lovely, another locale issue lurks; specifically I expect you are using a locale with 12-hour plus AM/PM time display (such as en_US.UTF-8). Said AM/PM causes mpstat's %idle column to become number 13 rather than 12...

Please try

Code: Select all

LC_ALL=C mpstat 1 | awk -v x=50 'NR > 3 { if ($12 > 100-x) exit }'
If still not simply visually inspect the output of just mpstat and LC_ALL=C mpstat but I rather trust that will be it. Note, making the $12 be $13 would be the worse solution since then the same line does not work in a 24-hour locale.

Of course by the way also feel free to "do the math" yourself: LC_ALL=C mpstat 1 | awk 'NR > 3 { if ($12 > 50) exit }'. I just took your "x" literally...

Re: Bash script question...

Posted: Wed Sep 26, 2018 1:32 pm
by invex
rene wrote:
Wed Sep 26, 2018 1:24 pm
invex wrote:
Wed Sep 26, 2018 12:13 pm
Maybe I'm doing something wrong but the following command never exits (should exit if CPU is under 50%):
Lovely, another locale issue lurks; specifically I expect you are using a locale with 12-hour plus AM/PM time display (such as en_US.UTF-8). Said AM/PM causes mpstat's %idle column to become number 13 rather than 12...

Please try

Code: Select all

LC_ALL=C mpstat 1 | awk -v x=50 'NR > 3 { if ($12 > 100-x) exit }'
If still not simply visually inspect the output of just mpstat and LC_ALL mpstat but I rather trust that will be it. Note, making the $12 be $13 would be the worse solution since then the same line does not work in a 24-hour locale.

Of course by the way also feel free to "do the math" yourself: LC_ALL=C mpstat 1 | awk 'NR > 3 { if ($12 > 50) exit }'. I just took your "x" literally...
I use 24h time format but now it works like a charm :)
Thank you VERY much!

Re: Bash script question...

Posted: Wed Sep 26, 2018 1:37 pm
by gm10
rene wrote:
Wed Sep 26, 2018 1:24 pm
Lovely, another locale issue lurks; specifically I expect you are using a locale with 12-hour plus AM/PM time display (such as en_US.UTF-8). Said AM/PM causes mpstat's %idle column to become number 13 rather than 12...
Other than your locale approach you could also:

Code: Select all

S_TIME_FORMAT=ISO mpstat 1

Re: Bash script question...

Posted: Wed Sep 26, 2018 1:47 pm
by rene
Was somewhat interesting running into two separate locale issues. But as to you using 24-hour time... In the GUI maybe, but I'm after this confirmation rather certain that you see AM/PM time when you type date into a terminal.

Tangent, but if you want to fix that: in Cinnamon, go into Preferences -> Languages and set (at least) "Region" to your correct region, possibly after installing the to said region corresponding "Language support" if not installed already -- assuming, of course, that your region is at the moment not in fact set correctly. If you are for example in the US but have set the GUI clock to 24-hour as personal preference there's nothing to fix. You'd need to logout and back in after making changes there.

gm10: not really, since that reintroduces the original potential issue with "12.34" versus "12,34".

Re: Bash script question...

Posted: Wed Sep 26, 2018 1:54 pm
by gm10
rene wrote:
Wed Sep 26, 2018 1:47 pm
gm10: not really, since that reintroduces the original potential issue with "12.34" versus "12,34".
I just meant with respect to your am/pm troubles. I'm surprised awk wouldn't apply the same locale setting as mpstat but I haven't looked into that so I didn't mean to comment on that (hadn't even read that part of the thread, sry ;)).

Re: Bash script question...

Posted: Wed Sep 26, 2018 2:37 pm
by rene
gm10 wrote:
Wed Sep 26, 2018 1:54 pm
I'm surprised awk wouldn't apply the same locale setting as mpstat [ ... ]
It's commented on on that linked page of the GNU awk manual: https://www.gnu.org/software/gawk/manua ... onversions.

I.e., it used to and does so now when supplied the --posix or --use-lc-numeric parameters but changed the default in response to Europeans complaining that data using period as decimal point -- itself no doubt having come about as a result of (much) earlier and/or other versions of awk and other tools not adapting to locale -- was now mismatched. Locale issue are fun like that; and that is then also the reason I didn't suggest the mentioned parameters to awk rather than LC_NUMERIC/LC_ALL for mpstat: when working non-interactively it pays to be as locale-neutral as possible; to use the C locale whenever potentially useful.

Re: Bash script question...

Posted: Wed Sep 26, 2018 4:33 pm
by gm10
rene wrote:
Wed Sep 26, 2018 2:37 pm
I.e., it used to and does so now when supplied the --posix or --use-lc-numeric parameters but changed the default in response to Europeans complaining that data using period as decimal point -- itself no doubt having come about as a result of (much) earlier and/or other versions of awk and other tools not adapting to locale -- was now mismatched. Locale issue are fun like that; and that is then also the reason I didn't suggest the mentioned parameters to awk rather than LC_NUMERIC/LC_ALL for mpstat: when working non-interactively it pays to be as locale-neutral as possible; to use the C locale whenever potentially useful.
Thanks for that summary, will try to keep that in mind, am not using awk that often. European data using a period is often done expressly for compatibility reasons, I tend to do that as well - it's a bit ironic that that can lead to incompatibilities then instead, it's true. And parsing locale-dependent numerical data is a pain. Dot and comma are easy enough, but then you get the Russians and some Eastern European countries which use a space as a thousand separator and it gets really messy...