You are not logged in.

#1 2019-09-26 18:49:55

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 1,932
Website

Launch function at 'exact' real-clock-minute change? (bash)

So I'd like to run the function 'thecode' at more or less exact and each real-clock minute change and this is the horror I came up with:


1.

#!/bin/bash

# waitUntil
# This should run 'thecode' at approximate real-clock minute change.

update() {
    # minutes from real clock without 0 padding
    oldmin="$(date +%-M)"
    newmin="$oldmin"
    #echo "$newmin"
}

waitUntil() {
while (( oldmin == newmin ))
do
  sleep 1
  newmin="$(date +%-M)"
done
}

thecode() {

    date +%H%M

}

while true
do

    thecode
    update
    waitUntil

done

Anything better, smaller, simpler, more precise?

Observations:
- With sleep set to 0.3 I'am faster to update than tty-clock.

2.

{ while true ; do date +%H%M ; sleep 0.1 ; done } | uniq 

Nice and short, but not sure how to call a function from this.

Last edited by brontosaurusrex (2019-09-26 19:17:14)

Offline

#2 2019-09-26 18:54:05

bigbenaugust
Member
From: unc.edu / the 919 / KIGX
Registered: 2017-05-20
Posts: 171

Re: Launch function at 'exact' real-clock-minute change? (bash)

Why would you not use at?
https://linux.die.net/man/1/at


--Ben
BL / MX / Raspbian... and a whole bunch of RHEL boxes. :)

Offline

#3 2019-09-26 19:05:36

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 1,932
Website

Re: Launch function at 'exact' real-clock-minute change? (bash)

Dunno, forgot to mention: every minute. An example?

at next minute <<< "echo 'woot'"

seems to echo in background.

Last edited by brontosaurusrex (2019-09-26 19:06:42)

Offline

#4 2019-09-26 19:25:46

bigbenaugust
Member
From: unc.edu / the 919 / KIGX
Registered: 2017-05-20
Posts: 171

Re: Launch function at 'exact' real-clock-minute change? (bash)

Oh, every minute? Well then.


--Ben
BL / MX / Raspbian... and a whole bunch of RHEL boxes. :)

Offline

#5 2019-09-26 19:39:54

twoion
ほやほや
Registered: 2015-08-10
Posts: 2,553

Re: Launch function at 'exact' real-clock-minute change? (bash)

@Bronto, your concept of computing the time till the next wake and then suspending execution for the time difference is kinda what a lot of the C/C++ implementations of the concept do (just at higher granularity, and there are functions to do this at repeating intervals, that is at the next full minute then every minute thereafter). Very interesting stuff; The Linux Programming Interface has a whole chapter on this, both on Linux timers and POSIX stuff.

That being said. Is cron not exact enough for you? It should be able to execute commands every minute. Does it not hit the 0s mark? Alternatively and with more bling, systemd timers can be set up for users and run at periodic intervals just like cron.


At the end of the river the sundown beams

Online

#6 2019-09-26 19:48:27

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 1,932
Website

Re: Launch function at 'exact' real-clock-minute change? (bash)

@twoion
I'am sure cron is up to the task, just not sure how to make that work in like interactive terminal? (Just trying to update figlet clock at almost exact minute mark at the moment).

Maybe I'll try version 1b to make it work with some sort of variable granularity (with seconds). For example
if seconds > 50 decrease sleep to 0.3 s (before that can be 10 s).

Last edited by brontosaurusrex (2019-09-26 19:54:03)

Offline

#7 2019-09-27 07:17:44

ohnonot
...again
Registered: 2015-09-29
Posts: 4,163
Website

Re: Launch function at 'exact' real-clock-minute change? (bash)

You want the command to execute exactly when the minute changes.
Sounds like a job for cron; but it might not be installed anymore on a systemd system. Systemd timer then?
See this: https://stackoverflow.com/questions/173 … riodically

'watch' also has a '-p' option - it might be precise enough that, if you get the start time right, it will run with "clock precision" from then on?

Offline

#8 2019-09-27 08:53:04

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 1,932
Website

Re: Launch function at 'exact' real-clock-minute change? (bash)

Found https://github.com/caronc/datetools, so

1b.

#!/bin/bash

#set -x

# waitUntil2, using https://github.com/caronc/datetools dateblock
# This should run 'thecode' at approximate real-clock minute change.

waitUntil() {

    dateblock -n /1

}

thecode() {

    date +%H%M | toilet -f 3x3
    
}

while true
do

    thecode
    waitUntil

done

Offline

#9 2019-09-29 08:04:53

ohnonot
...again
Registered: 2015-09-29
Posts: 4,163
Website

Re: Launch function at 'exact' real-clock-minute change? (bash)

brontosaurusrex wrote:

Very nice! The beauty of FOSS - there's a solution to almost everything.

Offline

#10 2019-09-30 07:29:26

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 1,932
Website

Re: Launch function at 'exact' real-clock-minute change? (bash)

@twoion

This one ? http://4.bp.blogspot.com/_WgybbNCbhU0/T … ax-red.jpg

In this authoritative work, Linux programming expert Michael Kerrisk provides detailed descriptions of the system calls and library functions that you need in order to master the craft of system programming

3.
Crontab version might be done via some dummy file update.

Last edited by brontosaurusrex (2019-09-30 09:04:44)

Offline

#11 2019-09-30 10:12:21

earlybird
ほやほや
Registered: 2015-12-16
Posts: 737
Website

Re: Launch function at 'exact' real-clock-minute change? (bash)

brontosaurusrex wrote:

Yes.

Offline

#12 2019-09-30 19:53:25

malm
jgmenu developer
Registered: 2016-10-13
Posts: 531
Website

Re: Launch function at 'exact' real-clock-minute change? (bash)

Yes. Definitely a good book.

Examples available online.
http://man7.org/tlpi/code/online/all_fi … apter.html

Ch23

Ch63 see select()

Offline

#13 2019-10-01 13:43:51

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 1,932
Website

Re: Launch function at 'exact' real-clock-minute change? (bash)

Arrived at ch02, 'what is a process, what are kernel and user space and ...'.

Offline

Board footer

Powered by FluxBB