When I shutdown and power off the Raspberry Pi 3b and then power it up again the next day, I get wrong starting date/time printed when checking sudo systemctl status DustSensor
. The printed date/time is from the date/time of the shutdown.
How can I delay the startup of services until the date/time is corrected from the NTP?
At shutdown and power off:
Feb 23 21:09:24 rp3b systemd[1]: Stopping DustSensor.service...
At startup (power on) the next day, still approx. the old date/time:
Feb 23 21:09:35 rp3b systemd[1]: Started DustSensor.service.
A bit later the date/time is corrected, but sudo systemctl status DustSensor
gives the old date/time (not shown here):
Feb 23 21:09:51 rp3b systemd[826]: Started D-Bus User Message Bus.
Feb 24 09:17:48 rp3b systemd[1]: Time has been changed
Feb 24 09:17:48 rp3b systemd-timesyncd[277]: Synchronized to time server [2a01:4f8:c17:b8f::2]:123 (2.debian.pool.ntp.org).
Feb 24 09:17:48 rp3b systemd[1]: apt-daily.timer: Adding 1h 24min 33.643221s random time.
Feb 24 09:17:48 rp3b systemd[1]: apt-daily-upgrade.timer: Adding 22min 10.691517s random time.
Feb 24 09:17:48 rp3b systemd[826]: Time has been changed
Feb 24 09:17:48 rp3b systemd[1]: Reached target Network is Online.
Here's my DustSensor.service file:
[Service]
ExecStart=/home/pi/SDS011_Feinstaub_Sensor.py
WorkingDirectory=/home/pi
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=DustSensor
User=pi
Group=pi
[Install]
WantedBy=multi-user.target
With the suggestion and the example hello.service:
[Unit]
Description=Hello After Time Sync
After=time-sync.target
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/echo "$(date) Hello world, just started"
[Install]
WantedBy=multi-user.target
I get this where only after starting the hello.service the date/time is updated
journalctl -b | egrep '(ello|anged|ync)'
Feb 28 10:48:54 rp3b systemd[1]: Starting Network Time Synchronization...
Feb 28 10:48:55 rp3b systemd[1]: Started Network Time Synchronization.
Feb 28 10:48:55 rp3b systemd[1]: Reached target System Time Synchronized.
Feb 28 10:49:00 rp3b systemd[1]: Starting Hello After Time Sync...
Feb 28 10:49:00 rp3b systemd[1]: Started Hello After Time Sync.
Feb 28 10:49:11 rp3b bluetoothd[570]: Failed to obtain handles for "Service Changed" characteristic
Feb 28 11:35:51 rp3b systemd[1]: Time has been changed
Feb 28 11:35:51 rp3b systemd[499]: Time has been changed
Feb 28 11:35:51 rp3b systemd-timesyncd[271]: Synchronized to time server [2a02:2028:ff01:14::13]:123 (2.debian.pool.ntp.org).
Also this shows the wrong starting time:
sudo systemctl status hello
\u25cf hello.service - Hello After Time Sync
Loaded: loaded (/etc/systemd/system/hello.service; enabled; vendor preset: enabled)
Active: active (exited) since Thu 2019-02-28 10:49:00 CET; 1h 16min ago
Process: 355 ExecStart=/bin/echo $(date) Hello world, just started (code=exited, status=0/SUCCESS)
Main PID: 355 (code=exited, status=0/SUCCESS)
CGroup: /system.slice/hello.service
Feb 28 10:49:00 rp3b systemd[1]: Starting Hello After Time Sync...
Feb 28 10:49:00 rp3b systemd[1]: Started Hello After Time Sync.