How is a systemd service unit activated by a timer unit

systemd

Given a unit file foo.service

# /etc/systemd/system/foo.service
[Service]
ExecStart=/opt/bin/foo --prod

And a unit file foo.timer

# /etc/systemd/system/foo.timer
[Timer]
OnCalendar=*-*-* 12:00:00

[Install]
WantedBy=timers.target
  • What happens at 12:00 every day if foo.service is off? (it will probably be started)
  • What happens at 12:00 every day if foo.service is already on? (will it be restarted?)

From reading man systemd.timer

For each timer file, a matching unit file must exist, describing the
unit to activate when the timer elapses. By default, a service by the
same name as the timer (except for the suffix) is activated. Example: a
timer file foo.timer activates a matching service foo.service. The unit
to activate may be controlled by Unit= (see below).

  • What does it mean to activate foo.service? Does it mean restart or verify that it's already started?

Best Answer

Normally the systemd service of a timer is a oneshot or simple (default) start-up type, which means the service runs a command and exits afterwards.
During the execution of the command given with ExecStart, the unit has the state active (running) and on exit something like inactive (dead).

When using timers you do not activate the service with systemctl enable --now foo.service but rather enable the corresponding timer unit systemctl enbale --now foo.timer.
What the timer does is more or less a systemctl start foo.service at the time given.


  • What happens at 12:00 every day if foo.service is off? (it will probably be started)

The foo.service will be started but is not enabled (auto-start at boot).

  • What happens at 12:00 every day if foo.service is already on? (will it be restarted?)

If the service is in an active (running) state nothing will happen. systemd won't try to start a service if that is already running.

  • What does it mean to activate foo.service? Does it mean restart or verify that it's already started?

To activate a service means to start it, e.g. systemctl start foo.service.