You are not logged in.

#1 2012-03-05 11:49:44

Lockheed
Member
Registered: 2010-03-16
Posts: 1,550

Runnin a script every 1-2 seconds

I am trying to get one script to run every 2 (or 1) seconds, but how can I do it?
Cron allow 1 minute as a minimal value. I also tried something like

while true ; do ./myscript & ; sleep 2; done

but all I get when I run it is:

bash: syntax error near unexpected token `do'

Why do I need it to run so often? Because I need to see current CPU voltage which I get by running

rdmsr -0 0x198 > /tmp/voltage 

and then reading the value into conky.

rdmsr requires sudo, so letting conky do it directly is not an option.

Offline

#2 2012-03-05 12:00:32

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Runnin a script every 1-2 seconds

Drop the '&' i.e. move it to the script.

Last edited by karol (2012-03-05 12:00:55)

Offline

#3 2012-03-05 12:03:16

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,456
Website

Re: Runnin a script every 1-2 seconds

I'm not sure why you get the syntax error near "do".  I get the error at ";" - presumably the one after the ampersand.

Is there a need for  'myscript' to be backgrounded?  If so try

while true; do ( ./myscript & ) ; sleep 2; done

This should run error-free (assuming 'myscript' is error free) but it seems like a very bad idea.  Any lag in 'myscript' could create a pile up of subshells/threads.

If it is not backgrounded it should still do what you want, and will ensure one instance finishes before the next starts.

I'm also not sure why the loop shouldn't just go in the script you are calling, eg

#!/bin/bash

while true
do
    rdmsr -0 0x198 > /tmp/voltage
    sleep 2
done

This should be far more efficient and will contain any error in a single process.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#4 2012-03-05 12:04:39

Lockheed
Member
Registered: 2010-03-16
Posts: 1,550

Re: Runnin a script every 1-2 seconds

$ while [ 1 ] ; do /home/xyz/.scripts/read_cpu_voltage ; sleep 2; done 
rdmsr: open: Permission denied
$ sudo while [ 1 ] ; do /home/xyz/.scripts/read_cpu_voltage ; sleep 2; done 
bash: syntax error near unexpected token `do'

Last edited by Lockheed (2012-03-05 12:04:58)

Offline

#5 2012-03-05 12:09:16

Lockheed
Member
Registered: 2010-03-16
Posts: 1,550

Re: Runnin a script every 1-2 seconds

Trilby wrote:

This should be far more efficient and will contain any error in a single process.

Thanks. That does exactly what I needed. Is there any performance penalty of running this simple script every 1-2 seconds?

I have a subsequent problem now because I am trying to read this value into conky and calculate it like this:

	   ${execi 60 ${exec cat /tmp/voltage} | awk --non-decimal-data '{s="0x" substr($1,length($1)-1) ; print 0.7125+s*0.0125}'}

but it doesn't display anything.

Last edited by Lockheed (2012-03-05 12:11:33)

Offline

#6 2012-03-05 12:42:04

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,456
Website

Re: Runnin a script every 1-2 seconds

$ sudo while [ 1 ] ; do /home/xyz/.scripts/read_cpu_voltage ; sleep 2; done 
bash: syntax error near unexpected token `do'

Yup.  Bash reads this as `sudo while [1]`  ... .... then when that potentially never ending priveledged process lingers, `do /home/...` hence the unexpected 'do'.

Keep it all in one script and it's easier to contain and track down errors.

There are probably pros and cons to consider with putting the sudo inside the while loop of the script, or calling the looping script with sudo.  I'd prefer the latter myself, but I'm no security expert.  Theoretically this *might* be considered undesirable by some as it leaves a priveledged script haning around in a wait state - but that's basically what cron jobs do anyways.  Having the sudo outside the script may (note *may*) have a performance benefit as it does not need to repeatedly elevate premissions every two seconds.

As for all that awk processing, I'd also move that from conky and into the bash script where you can work with it a bit more easily while taking advantage of built in bash string processing and even arithmetic if you'd like.

eg, this is a script that could be called with `sudo <scriptname>`

#!/bin/bash

while true
do
    RAW_OUTPUT=`rdmsr -0 0x198`
    # do something with $RAW_OUTPUT here to make PROCESSED_OUTPUT
    #    example: ${RAW_OUTPUT:-2:2}
    #         returns the last two characters of the string (I saw this step in your other thread).
    echo $PROCESSED_OUTPUT > /tmp/voltage
    sleep 2
done

EDIT: given the hex + floating point math, awk would probably be better than bash builtins.  But still, it'd be better to have that all in the script, then just have conky display the contents of the file.

I'm not sure why you have nested exec's in your conky - I don't even know what the expected output would be, so I can't say why it's not providing any.  *Simplify* first, then problems are easier to solve.  [Oh great, I sound like my junior high math teacher!]

Put all the processing in the shell script, then you can test it's output independent of conky.

As an added bonus, this would be far more portable.  You could reuse output of the same script for other system monitors, info bars, etc, if you ever switch.

EDIT AGAIN:  new version

#!/bin/bash
while true
do
    rdmsr -0 0x198 | awk --non-decimal-data '{s="0x" substr($1,length($1)-1) ; print 0.7125+s*0.0125}' > /tmp/voltage
    #   or if you'd rather not call the script itself with sudo:
    #sudo rdmsr -0 0x198 | awk --non-decimal-data '{s="0x" substr($1,length($1)-1) ; print 0.7125+s*0.0125}' > /tmp/voltage
    sleep 2
done

NOTE: I have not used this sort of string -> hex -> calculation in awk, so I can't vouche for that part, but it looks sensible enough.

Last edited by Trilby (2012-03-05 12:58:18)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#7 2012-03-05 13:33:58

Lockheed
Member
Registered: 2010-03-16
Posts: 1,550

Re: Runnin a script every 1-2 seconds

Trilby, I appreciate your help. Everything is working now splendidly.

Is there any performance penalty involved in running this script in 1-2 second loop?

Offline

#8 2012-03-05 13:53:21

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,456
Website

Re: Runnin a script every 1-2 seconds

Performance penalty compared to what?

Obviously having it run every 4 seconds instead of 2 would use less system resources (perhaps not much savings in memory use, but about half as much cpu).

Without clocking it myself I can't give specifics, but I'm fairly confident that having the loop inside the script will have a notable performance benefit over having a loop call the script.  One good rule of thumb for bash scripting (particularly relevant with loops) is to minimize the number of subprocesses that must be started.  A loop that calls a script would start and stop new instances of bash every two seconds.  Where the loop in the script should all run within a single bash instance.

Having conky get the hex code from the file then call another shell process to 'awk' it would be much more resource-heavy.  Conky would start a bash instance, cat the file, get the results, pass those results to yet another bash instance, that would in turn launch awk, process the number, and return the desired output to conky to display.

With the above script there is only one bash instance running.  Perhaps another if you use an 'exec cat' from conky, but as I recall I thought conky had a built in way to display the contents of a file without having to create a new bash instance.

Keep in mind, for most modern computers, such considerations can be fairly trivial.  I just have a bit of OCD for fine tuning everything when I can.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#9 2012-03-05 14:06:26

Lockheed
Member
Registered: 2010-03-16
Posts: 1,550

Re: Runnin a script every 1-2 seconds

What I meant was whether there is a noticeable difference from the CPU/RAM load between this script running every 1 second and every 60 seconds.

In conky I just used a 'cat' function so no calculations there.

Offline

#10 2012-03-05 15:19:37

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,456
Website

Re: Runnin a script every 1-2 seconds

You could try each and find out whether you find it noticeable.

There would be a difference, and it would certainly be noticed if you watch it in htop or a similar tool.  Whether this would actually impact the 'end-user experience' in any way is a bit more subjective and depends a good deal on your hardware and current set up.  I doubt having it run every second would have any notable effect.

For the heck of it, I just ran a similar script with a "sleep 1" and watched it in htop.  It was using "0.0%" cpu - or less than 0.05% I suppose, and 0.1% of memory.  I don't have a particularly powerful machine either (little netbook).


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#11 2012-03-05 17:17:24

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: Runnin a script every 1-2 seconds

The second half of this thread is the same topic as this one: https://bbs.archlinux.org/viewtopic.php?id=137057
Continue that discussion there. Thanks.


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

Board footer

Powered by FluxBB