Bash script question...
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Topics in this forum are automatically closed 6 months after creation.
Bash script question...
What command would I need to:
pause until CPU usage less than x%
TIA
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.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Re: Bash script question...
Install the sysstat package and try
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?
Code: Select all
LC_NUMERIC=C mpstat 1 | awk -v x=1 'NR > 3 { if ($12 > 100-x) exit }'
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.
Re: Bash script question...
Mod note:
Topic moved to Scripts & Bash.
Topic moved to Scripts & Bash.
If your issue is solved, kindly indicate that by editing the first post in the topic, and adding [SOLVED] to the title. Thanks!
Re: Bash script question...
Thank you very much.rene wrote: ⤴Wed Sep 26, 2018 6:21 am Install the sysstat package and tryin which of course the "1" in "x=1" is to be replaced.Code: Select all
LC_NUMERIC=C mpstat 1 | awk -v x=1 'NR > 3 { if ($12 > 100-x) exit }'
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?
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...
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 }'
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.
Re: Bash script question...
I use 24h time format but now it works like a charmrene wrote: ⤴Wed Sep 26, 2018 1:24 pmLovely, 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 tryIf still not simply visually inspect the output of justCode: Select all
LC_ALL=C mpstat 1 | awk -v x=50 'NR > 3 { if ($12 > 100-x) exit }'
mpstat
andLC_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...
Thank you VERY much!
Re: Bash script question...
Other than your locale approach you could also:
Code: Select all
S_TIME_FORMAT=ISO mpstat 1
Re: Bash script question...
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
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".
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...
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...
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...
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...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.