How to start user systemd.timer (s) automatically

configurationsystemd-timer

I have setup few user systemd.timer(s).

How to make them start automatically ? (either on system start or once user logged into X session).

After I restart of the system (even systemctl --user enable was run before restart, i.e. does not help) I have none running:

~$ systemctl --user enable {rsync_backup1,rsync_another_backup}.timer 
~$ systemctl --user list-timers --all
0 timers listed.

Here I commands I need to use to start them after :

~$ systemctl --user start {rsync_backup1,rsync_another_backup}.timer           
~$ systemctl --user list-timers --all
NEXT                          LEFT         LAST                          PASSED UNIT                          
Sun 2016-07-31 13:26:45 CEST  1h 16min ago Sun 2016-07-31 14:43:32 CEST  2s ago rsync_backup1
Sun 2016-07-31 13:26:45 CEST  1h 16min ago Sun 2016-07-31 14:43:32 CEST  2s ago rsync_another_backup

2 timers listed.
~$ 

Here is example how timers are currently configured :

$HOME/.config/systemd/user/rsync_backup1.service :

[Unit]
Description=rsync --delete /home/USER data to NASUSER@NAS

[Service]
Type=simple
ExecStart=/home/USER/scripts/rsync_backup1.sh

$HOME/.config/systemd/user/rsync_backup1.timer :

[Unit]
Description=Runs every 12 minutes rsync --delete /home/USER data to NASUSER@NAS

[Timer]
OnBootSec=12min
AccuracySec=10min
OnCalendar=*:0/12
Unit=rsync_backup1.service

[Install]
WantedBy=multi-user.target

P.S. Yes, I know I can drop commands, which start my timers into .bashrc or .xinit or my Window Manager "startup" scripts. What I am asking is: is there "systemd" "clean" way to define this to run after every restart (/login) ?

Best Answer

First, like others said, you'll want to have them be installed with WantedBy=default.target instead of WantdBy=multi-user.target. This is the standard way to allow enabling of user units.

But also, if you want them to start on boot, see this ArchWiki entry about starting systemd user units on boot instead of being tied to login.

In short:

# loginctl enable-linger username

(By the way, Unit=rsync_backup1.service is redundant since the timer and service file have the same base name)

Related Question