Re: [systemd-devel] timers always run when time changes

2015-05-18 Thread Lennart Poettering
On Fri, 01.05.15 09:35, Likang Wang (labor...@gmail.com) wrote:

 Hi all,
 
 I hava a timer with the fellowing setting:
 
 # cat /lib/systemd/system/updateimage.timer
 
 [Unit]
 Description=Update image
 DefaultDependencies=false
 
 [Timer]
 OnCalendar=*-*-* 02:00:00
 Persistent=false
 
 [Install]
 WantedBy=multi-user.target
 
 And I want the timer call the same-name service only on 02:00:00 daily.
 
 The entire system is running on an embedded box, and the system time will
 be set to 2008-01-01 00:00:00 after every reboot. My app running on the box
 will get the real time from my server and update time on the box after
 every booting.(I could not use NTP or systemd-timesyncd for some other
 reason)
 
 Here is my problem.
 When my app set the system time to real time, systemd will wake up the
 updateimage.timer and run the updateimage.service no matter what time it it
 now.The log looks like this:
 
 Jan  1 08:14:00 systemd[1]: xx.service: cgroup is empty
 Apr 30 21:04:00 systemd[1]: Time has been changed
 Apr 30 21:04:00 systemd[1]: Set up TFD_TIMER_CANCEL_ON_SET timerfd.
 Apr 30 21:04:00 systemd[1]: Timer elapsed on updateimage.timer
 Apr 30 21:04:00 systemd[1]: Trying to enqueue job
 updateimage.service/start/replace
 Apr 30 21:04:00 systemd[1]: Installed new job updateimage.service/start as
 9269
 
 What I want is the timer and the same-name service only run exactly on
 02:00:00 daily, but not when time changes. What should I do?

Well, the way this works is that when the timer unit is started
systemd determines the next time the unit shall elapse. Given that
your clock is initialized to 2008-01-01 00:00:00 this means it will
calculated 2008-01-02 02:00:00. Then, when you make the clock jump,
and it becomes 2015-04-30 21:04:00 systemd notes that the calculated
elapsing time is now already in the past and immediately dispatch the
timer unit.

In most cases this is arguably what you want since it gives you the
guarantee that your service is always run less time ago than the
interval you specified -- on the wallclock.

Now I can see that in you case this behaviour might not be
advisable. Two suggestions:

- consider using systemd-timesyncd for time synchronization. It
  implements sNTP and will sync the last known time to disk every time
  it gets an sNTP sync or the system is shut down. At boot it uses
  that time to reinitialize the clock, as early as possible, before
  NTP is done. THis will give you monotonic time which should solve
  your probelm.

- Introduce some flag file to conditionalize your service with
  ConditionPathExists= or so, so that it is only started when the
  clock was set at least once... Each time you sync the clock, create
  that file. if it is missing you hence know that the clock is not
  ready for syncing on...

Lennart

-- 
Lennart Poettering, Red Hat
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/systemd-devel


Re: [systemd-devel] timers always run when time changes

2015-04-30 Thread Cameron Norman
On Thu, Apr 30, 2015 at 6:35 PM, Likang Wang labor...@gmail.com wrote:
 Hi all,

 The entire system is running on an embedded box, and the system time will be
 set to 2008-01-01 00:00:00 after every reboot. My app running on the box
 will get the real time from my server and update time on the box after every
 booting.(I could not use NTP or systemd-timesyncd for some other reason)

Does the service file for this custom time syncing service have the
directive `Before=time-sync.target` ? If it does, are you sure that
when it is considered running by systemd that the time has indeed been
synced?

Cheers,
--
Cameron Norman
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/systemd-devel


Re: [systemd-devel] timers always run when time changes

2015-04-30 Thread Likang Wang
The custom time syncing service file does not have
Before=time-sync.target.

In fact, not only when the time changes by the custom time syncing service
,but also when I set the time manually by date -s 2015-05-01 11:08:00,
the timer and the same-name service will run.


Here is the custom time syncing service file:
#cat /lib/systemd/system/XX.service
[Unit]
Description=XXX Daemon
After=ppp.service

[Service]
Type=simple
ExecStart=/usr/lib/XX/xx/bin/xx
Restart=always


Here is the log when I set the time manually by date -s 2015-05-01
11:08:00
#systemctl status updateimage.timer -l
— updateimage.timer - Update image
   Loaded: loaded (/lib/systemd/system/updateimage.timer; disabled)
   Active: active (waiting) since Fri 2010-01-01 08:00:04 CST; 5 years 3
months ago

May 01 11:08:00 systemd[1]: Timer elapsed on updateimage.timer
May 01 11:08:00 systemd[1]: updateimage.timer changed waiting - running
May 01 11:08:00 systemd[1]: updateimage.timer got notified about unit
deactivation.
May 01 11:08:00 systemd[1]: updateimage.timer: Realtime timer elapses at
Sat 2015-05-02 02:00:00 CST.

#systemctl status updateimage.service -l
â— updateimage.service - Update image
   Loaded: loaded (/lib/systemd/system/updateimage.service; static)
   Active: inactive (dead) since Fri 2015-05-01 11:08:00 CST; 18min ago
  Process: 579 ExecStart=/bin/updateImage.sh (code=exited, status=0/SUCCESS)
 Main PID: 579 (code=exited, status=0/SUCCESS)

May 01 11:08:00 systemd[1]: Enqueued job updateimage.service/start as 1019
May 01 11:08:00 systemd[1]: Forked /bin/updateImage.sh as 579
May 01 11:08:00 systemd[1]: updateimage.service changed dead - start
May 01 11:08:00 systemd[579]: Executing: /bin/updateImage.sh
May 01 11:08:00 systemd[1]: Child 579 belongs to updateimage.service


--
laborish




2015-05-01 10:34 GMT+08:00 Cameron Norman camerontnor...@gmail.com:

 On Thu, Apr 30, 2015 at 6:35 PM, Likang Wang labor...@gmail.com wrote:
  Hi all,
 
  The entire system is running on an embedded box, and the system time
 will be
  set to 2008-01-01 00:00:00 after every reboot. My app running on the box
  will get the real time from my server and update time on the box after
 every
  booting.(I could not use NTP or systemd-timesyncd for some other reason)

 Does the service file for this custom time syncing service have the
 directive `Before=time-sync.target` ? If it does, are you sure that
 when it is considered running by systemd that the time has indeed been
 synced?

 Cheers,
 --
 Cameron Norman

___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/systemd-devel