How do I properly install a systemd timer and service?

To run a unit at specified times or intervals you need two units:

  • a service unit that defines what to run
  • a timer unit that defines when to run the service unit

By convention, the timer unit starts another unit with the same name, i.e. foo.timer starts foo.service. You can override this by defining the Unit=other.service attribute in the timer unit (like you did).

If both unit files are created and put in /etc/systemd/system you need to make systemd aware of them by issuing

systemctl daemon-reload

This makes systemd reload all unit files and re-consider their dependencies because systemd caches these files somehow. So whenever you change a unit file, this command is required.

After that you need to enable the timer unit:

systemctl enable foo.timer

This commands simply enables auto-start at boot time (but doesn't start the unit yet). Do not enable the service unit because that would mean to start the service at boot time (independent of any timer settings).

Now the next time you boot, the timer will be engaged. To start it immediately (without booting) you would run

systemctl start foo.timer

From now on, the timer unit will start the service unit whenever the time comes. You can combine enabling and starting with

systemctl enable --now foo.timer

You can (and should) leave the service unit alone, i.e. neither enable nor start it. This is now handled by the timer unit.

To see the current status of both timer and service, issue

systemctl status foo.timer foo.service

To summarize

  • systemctl enable/disable controls the behaviour when booting
  • systemctl start/stop controls the behaviour right now
  • enable does not imply start (neither does disable imply stop). This can be overriden with the --now switch.
  • only enable and start the timer unit, not the service unit
  • issue systemctl daemon-reload whenever you edit the unit files

Further reading:

  • timers: https://www.freedesktop.org/software/systemd/man/systemd.timer.html
  • services: https://www.freedesktop.org/software/systemd/man/systemd.service.html
  • units (applies to both timers and services): https://www.freedesktop.org/software/systemd/man/systemd.unit.html
  • a related post