Trigger another systemd unit to start before timer unit starts?

2

1

I have a RPi3. Since it doesn't have a hardware clock, I have a service for tlsdate that syncs time with a local server. (This is triggered on every boot. regularly because of the frequent power cuts).

I have one more timer mysync.timer that starts mysync.service that syncs my calendar. This uses OAuth and if there is a time mismatch, it errors out.

Is there any way to start the tlsdate.service before mysync.service, every time mysync.service runs?

I looked at systemd documentation and found Before= and After=, but I am not sure how to use that with timers.

Karthik Nishanth

Posted 2017-09-16T09:38:11.453

Reputation: 123

1Timers merely schedule the startup, but they do not change how the .service unit behaves otherwise. – user1686 – 2017-09-16T10:44:53.490

Answers

5

All you need to do is add an After and Wants in mysync.service:

After=tlsdate.service
Wants=tlsdate.service

This will cause tlsdate.service to start before mysync.service. If you want to be sure that tlsdate has completed before mysync, you might want to make sure tlsdate is Type=oneshot, as opposed to the default of Type=simple.

The distinction between the two is After specifies the order if both are started at the same time, while Wants causes tlsdate to start when mysync is started. So you need both, to specify the order and the dependency.


If you want a hard dependency, i.e. mysync should fail if tlsdate cannot be started, then you should use Requires:

After=tlsdate.service
Requires=tlsdate.service

You should be able to ignore the timer entirely. The timer will start mysync.service but what you really want is a dependency in mysync.service that tlsdate.service is started first, regardless of whether it's triggered through a timer or not.

Bob

Posted 2017-09-16T09:38:11.453

Reputation: 51 526