systemd unit "Before=" with timer

In my system I have b.service activated by b.timer. I want another service (a.service) that start before b.service. I cant change b.service or b.timer because are not mine. I’ve putte Before=b.service in a.service but the timer start b.service without starting a.service.

Asked By: Andrea Romano

||

You can mark a.service as RequiredBy b.service.

Make a.service look like:

[Unit]
Before=b.service

[Service]
Type=exec
ExecStart=...

[Install]
RequiredBy=b.service

And then:

systemctl enable a.service

Now whenever b.service starts — either via a timer or via systemctl start — your new a.service will start first.

Answered By: larsks

The Before directive is only used during startup. It’s only used for to determine the orders of the services.

In order to achieve what you need, add the following section to to your a.service:

[Install]
WantedBy=b.service

Then run:

systemctl enable a.service

This will ensure that when b.service is supposed to get started, a.service will be started before.

Answered By: aviro