You are not logged in.

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

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,746

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: the 704 / KCLT
Registered: 2017-05-20
Posts: 179

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: 2,746

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: the 704 / KCLT
Registered: 2017-05-20
Posts: 179

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:48:27

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,746

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

@nobody
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

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

ohnonot
...again
Registered: 2015-09-29
Posts: 5,592

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

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

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,746

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

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

ohnonot
...again
Registered: 2015-09-29
Posts: 5,592

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

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

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,746

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

@nobody

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

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

malm
jgmenu developer
Registered: 2016-10-13
Posts: 735
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

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

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,746

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