LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   query cpu load every x secs and beep when falls (https://www.linuxquestions.org/questions/programming-9/query-cpu-load-every-x-secs-and-beep-when-falls-375414/)

Melsync 10-21-2005 09:12 AM

query cpu load every x secs and beep when falls
 
I run a program that executes cpu and memory hungry jobs. I launch them to come back to the box to see if they are finished.
I'd like to write a simple bash script running in the background that notifies me when the jobs are finished.
I guess that I'll combine a simple
echo -e "\a"
with regular checks of loads
ps -eao "pcpu" | awk '{a+=$1} END {print a}'
ps -eao "pmem" | awk '{a+=$1} END {print a}'
How do I run the last two commands every five seconds, for instance?
Thanks!

Quigi 10-21-2005 10:32 AM

If once a minute was good enough, you could make the notifier a cron job (with * * * * *). But probably you'd want the beeping to stop when you're back at the box and got the results.

The simplest might be to enter the big job and the beeper together, e.g.,
Code:

bigjob; echo -e "\a"
Yet another approach would be to schedule the beeper with "batch". This uses the feature that batch will only start your job when the load average drops below 0.8 (or the value specified in the invocation of atrun. That way you don't even need to watch CPU levels, or compare the output of your awk summation to some threshold.

BTW, to just see the load, you could use "uptime".

Quigi 10-21-2005 10:37 AM

P.S.,

You asked about doing somehting every 5 seconds. You can put the command "sleep 5" together with your commands in a loop. E.g., in csh
Code:

while (1)
    ps -eao ...
    sleep 5
end

You indicate that you prefer bash, so your exact syntax will be different. But you can still use sleep.
BTW, this won't run exactly every 5 seconds, because ps and the loop take some time too.

Melsync 10-21-2005 10:43 AM

its a GUI-only program
 
Oh, sorry, the program is a GUI without command line control. It manipulates digital pictures and I can only know when the jog is over by inspecting the screen, or indirectly by checking the cpu and memory load.
I'd need to apply awk to uptime, as well, right?
Thanks.

Quigi 10-21-2005 11:37 AM

I mentioned uptime in case you wanted to look at the load averages just for your information (BTW, top shows them also). If you want to use the output in a script, indeed you have to pick out the number using awk (or cut, or perl, or assigning to a variable then indexing). And then you have to code a comparison of that value to a threshold, and based on the outcome beep or not.

But batch does it all for you -- waiting and checking load, until it drops below 0.8. Then it runs your script, which only needs to beep.

Melsync 10-21-2005 12:12 PM

man batch |lpr :)
Thanks a lot.


All times are GMT -5. The time now is 02:53 AM.