Bash script question...

Forum rules
Before you post please read how to get help
Post Reply
User avatar
invex
Level 2
Level 2
Posts: 78
Joined: Sun Aug 14, 2016 12:38 pm

Bash script question...

Post by invex » Tue Sep 25, 2018 5:46 pm

What command would I need to:
pause until CPU usage less than x%

TIA


rene
Level 8
Level 8
Posts: 2172
Joined: Sun Mar 27, 2016 6:58 pm

Re: Bash script question...

Post by rene » 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? :?
Last edited by rene on Wed Sep 26, 2018 6:34 am, edited 2 times in total.

User avatar
Moem
Level 16
Level 16
Posts: 6760
Joined: Tue Nov 17, 2015 9:14 am
Location: The Netherlands
Contact:

Re: Bash script question...

Post by Moem » Wed Sep 26, 2018 6:32 am

Mod note:
Topic moved to Scripts & Bash.
Image

If your issue is solved, kindly indicate that by editing the first post in the topic, and adding [SOLVED] to the title. Thanks!

User avatar
invex
Level 2
Level 2
Posts: 78
Joined: Sun Aug 14, 2016 12:38 pm

Re: Bash script question...

Post by invex » Wed Sep 26, 2018 12:13 pm

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

rene
Level 8
Level 8
Posts: 2172
Joined: Sun Mar 27, 2016 6:58 pm

Re: Bash script question...

Post by rene » 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=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...
Last edited by rene on Sun Sep 30, 2018 9:50 am, edited 1 time in total.

User avatar
invex
Level 2
Level 2
Posts: 78
Joined: Sun Aug 14, 2016 12:38 pm

Re: Bash script question...

Post by invex » Wed Sep 26, 2018 1:32 pm

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!

gm10
Level 10
Level 10
Posts: 3435
Joined: Thu Jun 21, 2018 5:11 pm

Re: Bash script question...

Post by gm10 » Wed Sep 26, 2018 1:37 pm

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

rene
Level 8
Level 8
Posts: 2172
Joined: Sun Mar 27, 2016 6:58 pm

Re: Bash script question...

Post by rene » Wed Sep 26, 2018 1:47 pm

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

gm10
Level 10
Level 10
Posts: 3435
Joined: Thu Jun 21, 2018 5:11 pm

Re: Bash script question...

Post by gm10 » Wed Sep 26, 2018 1:54 pm

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 ;)).

rene
Level 8
Level 8
Posts: 2172
Joined: Sun Mar 27, 2016 6:58 pm

Re: Bash script question...

Post by rene » Wed Sep 26, 2018 2:37 pm

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.

gm10
Level 10
Level 10
Posts: 3435
Joined: Thu Jun 21, 2018 5:11 pm

Re: Bash script question...

Post by gm10 » Wed Sep 26, 2018 4:33 pm

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

Post Reply

Return to “Scripts & Bash”