1

i'm using rapberry Pi 3 with Stretch (debian-based) and timesyncd as NTP client.

Pi is being switched of and on periodically and connects to internet through usb modem, then synchronizes time. syslog cut:

Oct  3 12:36:02 raspberrypi pppd[2756]: primary   DNS address 10.74.32.5
Oct  3 12:36:02 raspberrypi pppd[2756]: secondary DNS address 10.74.32.6
Oct  3 12:36:02 raspberrypi dnsmasq[607]: reading /run/dnsmasq/resolv.conf
Oct  3 12:36:02 raspberrypi dnsmasq[607]: using nameserver 8.8.8.8#53
Oct  3 12:36:02 raspberrypi dnsmasq[607]: using nameserver 10.74.32.5#53
Oct  3 12:36:02 raspberrypi dnsmasq[607]: using nameserver 10.74.32.6#53
Oct  3 12:36:02 raspberrypi dnsmasq[607]: using nameserver 8.8.8.8#53
Oct  3 12:36:02 raspberrypi PPP: Modem connected
Oct  3 12:36:02 raspberrypi kernel: [  617.235549] nf_conntrack version 0.5.0 (15360 buckets, 61440 max)
Oct  3 12:36:06 raspberrypi autossh[2877]: starting ssh (count 1)
Oct  3 12:36:06 raspberrypi autossh[2877]: ssh child pid is 2878
Oct  4 02:47:23 raspberrypi systemd[2434]: Time has been changed
Oct  4 02:47:23 raspberrypi systemd-timesyncd[274]: Synchronized to time server 91.236.251.24:123 (3.debian.pool.ntp.org).
Oct  4 02:47:23 raspberrypi systemd[1]: Time has been changed

I need to run some script automatically only after successful system time synchronization. i.e. at line

Oct 4 02:47:23 raspberrypi systemd-timesyncd[274]: Synchronized to time server 91.236.251.24:123 (3.debian.pool.ntp.org).

of the log above.

Could somebody advice? If timesyncd is not an option, may be you can advice another ntp client which allows event scripting? Or some another approach?

Thanks!

0

See if you have the ntpd-daemon running and manually update the time in your script and if successful run the rest:

!#/bin/bash

service ntp stop && ntpq -gq && service ntp start && continue

Of course this would need root rights for the updating.

Reference

  • may be I was not precise, I need it be done automatically, i.e. when time is synchronized - we automatically run script – DmitryD Oct 4 '18 at 15:43
  • @DmitryD well, the script then can be autorun (startup script) in the booting sequence (via systemd), Or should it not only run when the system is started but also check regularly for time syncs? – Fiximan Oct 4 '18 at 15:47
  • 1
    thanks for the idea! Indeed, I disabled timesyncd auto-start, then when connection established I automatically run ntpdate and after successful sync I fire my script. Then I start timesyncd to keep time synced – DmitryD Oct 5 '18 at 15:24
0

There was a proposed api to be notified of clock changes, but it has not been implemented. Looking through the timesyncd sources, it seems that when the clock is changed, the 2 files

/run/systemd/timesync/synchronized
/var/lib/systemd/timesync/clock

are "touched", i.e. their last-modified time is updated. If you create one of these directories (/run is recreated at each reboot), you could try using inotifywait to get events when the file changes, and run your program then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.