Wrong time after power off Topic is solved

Having issues with your DietPi installation, or, found a bug? Post it here.
huidbui25
Posts: 32
Joined: Tue May 07, 2019 7:07 pm
Location: DE

Wrong time after power off

Post by huidbui25 »

Hi!

After powering off my rPi for a few hours (e.g. night), DietPi doesn't get the new time when powered on again. Rebooting DietPi doesn't help. Wrong system time makes my DietPi behave as if there was no online connection (b/c unbound's DNS resolution fails).

My work around: I stop fake-hwclock.service, manually type in the new time, start fake-hwlock and all things work fine again instantly.

Question: Do I have to install NTP and use ntpd to get the new time automatically during boot up or is there anything I can do with the tools aboard DietPi?
User avatar
WarHawk
Posts: 610
Joined: Thu Jul 20, 2017 8:55 am

Re: Wrong time after power off

Post by WarHawk »

Yeah..is there a NTP setting to have it check at boot and every hour or so?
huidbui25
Posts: 32
Joined: Tue May 07, 2019 7:07 pm
Location: DE

Re: Wrong time after power off

Post by huidbui25 »

Yes, there is.

Try "ntpd -q -g [time server]", e.g. as a cronjob at reboot. But this needs ntp to be installed.

I am wondering though if there is a way to update the system time without having to install ntp.
User avatar
MichaIng
Site Admin
Posts: 2294
Joined: Sat Nov 18, 2017 6:21 pm

Re: Wrong time after power off

Post by MichaIng »

@huidbui25
Actually DietPi uses the by default integrated systemd-timesyncd to update system time on boot. Please check: dietpi-config > Advanced Options > Time sync mode

You can run manually what this does at boot (and daily if chosen): /DietPi/dietpi/func/run_ntpd 1
Verify that this succeeds. If this somehow fails on boot, you could try the daemon + drift mode to enable systemd-timesyncd as daemon.

fake-hwclock.service btw should not be active, is it? It runs as oneshot during veeery early boot time to set the system clock to the one saved on last shutdown, long before network is established. This emulates a real hwclock moreless fine. At least you will not face 1970s timestamps until network is up ;).
huidbui25
Posts: 32
Joined: Tue May 07, 2019 7:07 pm
Location: DE

Re: Wrong time after power off

Post by huidbui25 »

Thank you. I didn't know this command. I tried but unfortunately it doesn't work either.

It says right after reboot (and system time is wrong):

Code: Select all

root@DietPi:~# /DietPi/dietpi/func/run_ntpd 1
[  OK  ] DietPi-Run_NTPD | Root access verified.
[ INFO ] DietPi-Run_NTPD | Concurrent execution of DietPi-Run_NTPD detected: Retry (0/5), please wait...
[ INFO ] DietPi-Run_NTPD | Concurrent execution of DietPi-Run_NTPD detected: Retry (1/5), please wait...
[ INFO ] DietPi-Run_NTPD | Concurrent execution of DietPi-Run_NTPD detected: Retry (2/5), please wait...
[ INFO ] DietPi-Run_NTPD | Concurrent execution of DietPi-Run_NTPD detected: Retry (3/5), please wait...
[ INFO ] DietPi-Run_NTPD | Concurrent execution of DietPi-Run_NTPD detected: Retry (4/5), please wait...
[FAILED] DietPi-Run_NTPD | Canceled DietPi-Run_NTPD due to concurrent execution
DietPi pops up the following window afterwards:

Code: Select all

						 WARNING: Concurrent execution of DietPi-Run_NTPD detected                                                            │
                           │                                                                                                                      │
                           │ Please check if one of the following applies:                                                                        │
                           │  - This script already runs on another terminal/SSH session.                                                         │
                           │  - Currently a cron or systemd background job executes the script.                                                   │
                           │  - You started this script from within another DietPi program, causing a loop.                                       │
                           │                                                                                                                      │
                           │ Please assure that the concurrent execution has finished, before retrying, otherwise cancel this instance.           │
                           │                                                                                                                      │
                           │ The following info might help:                                                                                       │
                           │  1517 root     pts/0             \_ /bin/bash /DietPi/dietpi/func/run_ntpd 1                                         │
                           │  1541 root     pts/0                 \_ /bin/bash /DietPi/dietpi/func/run_ntpd 1                                     │
                           │  1006 root     ?             \_ /bin/sh -c /DietPi/dietpi/func/run_ntpd 1                                            │
                           │  1007 root     ?                 \_ /bin/bash /DietPi/dietpi/func/run_ntpd 1   
I tried with DietPi-Config TimeSync modes "Boot + Daily" (error messages see above) and "Custom" ... time update fails in both cases. My only guess would be that DietPi is continuosly trying to set the time (hence concurrent execution?!?!???) but fails due to impossible DNS resolution because unbound (my DNS resolver on my Raspberry Pi) doesn't work with a wrongly set system time.

The working solution for me is a simple command in crontab:
@reboot /usr/bin/sntp -S 1.2.3.4
whereas 1.2.3.4 is the IP-address of a time server (not the domain name)

This just as feedback for you. I'm on DietPi 6.22.3, upgraded to Buster sid from Strech stable.

PS.: And yes, fake-hwclock.service is active, but I am quite sure I didn't switch it on. It's enabled by default at each reboot as it appears to me.
User avatar
MichaIng
Site Admin
Posts: 2294
Joined: Sat Nov 18, 2017 6:21 pm

Re: Wrong time after power off

Post by MichaIng »

@huidbui25
Jep fake-hwclock should be active/enabled by default:
systemctl status fake-hwclock

Jep obviously time sync fails on boot, please check:
systemctl status systemd-timesyncd

If DNS resolving does not work reliable on your system then this is most likely the reason since default NTP servers are: [0-3].debian.pool.ntp.org

Open: dietpi-config > Network Options: Misc > NTP mirror
- Either choose "custom" to add your custom time server IP,
- or choose "gateway", if your router serves as NTP server, which is true in most cases and then the recommended setup,
- and replace your cron job by this.
Bart
Posts: 1
Joined: Tue Jun 04, 2019 3:50 pm

Re: Wrong time after power off

Post by Bart »

Hello there,

i recently set up an ODROID XU4 with dietpi and pi-hole with active dnssec. Due to the daily time-sync, the dns queries all fail (BOGUS) because dnssec needs a valid time but dietpi can't get a correct time because dns fails...

The reason I'm posting this here: The switch to "Gateway" in NTP Mirror helped to get rid of this problem and maybe its an option to ask for such a change in the pi-hole installation procedure?

Unfortunatly I have no idea what to do if there is no ntp server available, perhaps ataching a battery or an external RTC device helps.

Anyway: Thanks a lot for pointing me in the right direction here. You guys rock, dietpi is such a good OS for low budget hardware!
User avatar
johnvick
Legend
Posts: 693
Joined: Wed Jun 08, 2016 11:53 am
Location: New Zealand

Re: Wrong time after power off

Post by johnvick »

You're aware you can buy an RTC for the XU$from hardkernel?
baz123
Posts: 166
Joined: Thu Jan 12, 2017 9:23 pm

Re: Wrong time after power off

Post by baz123 »

I have been having some issues with the restoration of time on a Pi4 on boot on a Raspbian system and noted a similar problem may occur with DietPi.

Both fake-hwclock and timedatectl systems save the current datetime periodically (and also on shutdown), but I was finding that on boot, especially if there was a hard power off, the 2 were in conflict, both restoring different times. timedatectl is more sophisticated as the frequency of the check with an NTP server is determined by the clock drift and is my preferred option.

I have ended up removing fake-hwclock from the system as it was not needed as timedatectl was enabled. I also changed the PollIntervalMaxSec to 300 in /usr/lib/systemd/timesyncd.conf to ensure the last save time was very recent.

My suggestion is that DietPi should remove fake-hwclock and just rely on timedatectl to manage time when option 4 is selected.

A menu item could be offered to modify the poll period.
User avatar
MichaIng
Site Admin
Posts: 2294
Joined: Sat Nov 18, 2017 6:21 pm

Re: Wrong time after power off

Post by MichaIng »

@baz123
Many thanks for reporting. I remember reading through some Debian bug report about conflicts between fake-hwclock and systemd-timeyncd, not sure if there has been some solution to reasonably resolve the conflict on testing (Bullseye). The issue with purging fake-hwclock is:
1. DietPi by default has systemd-timesyncd disabled and only calls it on boot/daily/hourly (based on choice) to sync the time. As it consumes ~5 MiB RAM this is especially reasonable on low RAM devices.
2. Even if systemd-timesyncd is enabled (based on choice: daemon mode), it may prevent itself from doing anything if another time sync daemon is present, e.g. ntp or chrony. AFAIK it as well does not store the system time to disk then, leading to 1970s time on boot.

So as of above, I prefer to stay with fake-hwclock installed by default. It cannot break anything, just lead to a max 1h past system time on boot, compared to a current systemd-timesyncd store. That is acceptable and should not break any internal tasks until network time sync is started.

I was looking for a way to easily check systemd-timesyncd condition result. The systemd unit prevents to start if if finds any known time sync daemon. Would be great if we could apply NTP choice "custom" in such case automatically, but I did not find a beautiful way to check this without actually starting the service. If we found a nice way, we can then also reliably know if systemd-timesyncd is really running and hence know whether fake-hwclock can be removed or not.

... Ah, just found another topic about this on RPi forum: https://www.raspberrypi.org/forums/view ... 3#p1615843
systemd-timesyncd starts too late at boot to assure that all tasks that require it have a moreless current system time, in this case systemd-fsck, but there might be others. Makes sense since it is a time sync daemon which requires network, while restoring system time from a save file does not require network and is basically a different task. Both tasks must run at different times.
Just checked it on current Raspbian Bullseye:

Code: Select all

2020-05-18 14:52:24 root@micha:/mnt/sda# systemctl show -p Before,After systemd-timesyncd
Before=time-set.target sysinit.target shutdown.target
After=systemd-tmpfiles-setup.service system.slice systemd-sysusers.service tmp.mount systemd-remount-fs.service systemd-journald.socket -.mount
2020-05-18 14:52:32 root@micha:/mnt/sda# systemctl show -p Before,After fake-hwclock
Before=sysinit.target shutdown.target
After=systemd-journald.socket system.slice
systemd-timesyncd does not wait for network, but definitely waits for a few more services :thinking:.
Post Reply