In various examples, I have seen all of these different choices suggested:
- WantedBy=timers.target
- WantedBy=multi-user.target
- WantedBy=basic.target
- WantedBy=default.target
- WantedBy=mytimer.target (custom user-defined name)
However, in the examples I have found, no further explanation is offered.
The following pages also don't offer any explanation of WantedBy:
- https://www.freedesktop.org/software/systemd/man/systemd.timer.html
- https://www.freedesktop.org/software/systemd/man/systemd.time.html
I want to understand one simple* method I can use when needed to write a systemd timer instead of a cron job.
* Setting up a cron job is 1 line. Systemd timers involve writing two files and running one or two systemctl commands. But that fact alone is not necessarily what makes systemd timers harder than cron in my experience — it's the multitude of options and the (seeming) lack of clear documentation with simple examples that are fully explained.
Best Answer
For the simple use case, use
WantedBy=timers.target
. Seeman systemd.special
:Timers get a dependency of
Before=timers.target
by default. And, if you checkman bootup
, you'll see thatbasic.target
pulls intimers.target
as a dependency. So I thinkWantedBy=basic.target
would appear to work OK for most cases (same fordefault.target
, which is usuallymulti-user.target
orgraphical.target
, both of which come afterbasic.target
). But:So a more complicated timer that depends on some other service unit would be better off depending on
timers.target
rather than any of the others.