Bash script question...

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
User avatar
invex
Level 4
Level 4
Posts: 219
Joined: Sun Aug 14, 2016 12:38 pm

Bash script question...

Post by invex »

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

TIA
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
kukamuumuka

Re: Bash script question...

Post by kukamuumuka »

Code: Select all

cpulimit
rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: Bash script question...

Post 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? :?
Last edited by rene on Wed Sep 26, 2018 6:34 am, edited 2 times in total.
User avatar
Moem
Level 22
Level 22
Posts: 16193
Joined: Tue Nov 17, 2015 9:14 am
Location: The Netherlands
Contact:

Re: Bash script question...

Post by Moem »

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 4
Level 4
Posts: 219
Joined: Sun Aug 14, 2016 12:38 pm

Re: Bash script question...

Post 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 }'
rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: Bash script question...

Post 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...
Last edited by rene on Sun Sep 30, 2018 9:50 am, edited 1 time in total.
User avatar
invex
Level 4
Level 4
Posts: 219
Joined: Sun Aug 14, 2016 12:38 pm

Re: Bash script question...

Post 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!
gm10

Re: Bash script question...

Post 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
rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: Bash script question...

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

Re: Bash script question...

Post 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 ;)).
rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: Bash script question...

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

Re: Bash script question...

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

Return to “Scripts & Bash”