You are not logged in.

#1 2017-12-11 23:00:50

CrashLog
Member
From: Finland
Registered: 2013-01-13
Posts: 136

Using a systemd user timer to launch a network-dependent service

I'm using systemd version 235.38. I want to use rsync to take daily backups by using a systemd timer to launch a script.

I've enabled and started the timer with the --user switch. Everything works fine when the network is up, but if the network is down, like just after waking up from suspend, the service does not wait until a network connection is established. Reading stuff on the web indicated that adding the network-online.target lines to the service file would fix this, but that hasn't made any difference. Is there anything I can do to get the behavior I want?

The timer:

[Unit]
Description=Backup home directory

[Timer]
OnCalendar=daily
Persistent=true

[Install]
WantedBy=timers.target

And its associated service file:

[Unit]
Description=Backup home directory
Wants=network-online.target
After=network-online.target

[Service]
Type=oneshot
ExecStart=/home/<snip>/Documents/Repositories/home-backup/home-backup.sh

[Install]
WantedBy=multi-user.target

And finally the script:

#!/bin/bash

DATE=`date -Iminutes`
PTH="/home/<snip>"
EXCLFILE="/home/<snip>/Documents/Repositories/home-backup/exclude"
REMOTESERVER="<snip>"
REMOTEPATH="<snip>"
INCOMPLETE="<snip>"
COMPLETE="<snip>"

rsync -az --exclude-from=$EXCLFILE --link-dest=../full-backup \
    $PTH $REMOTESERVER:$REMOTEPATH/$INCOMPLETE && ssh $REMOTESERVER "mv $REMOTEPATH/$INCOMPLETE $REMOTEPATH/$COMPLETE \
    && rm -rf $REMOTEPATH/full-backup \
    && ln -s $REMOTEPATH/$COMPLETE $REMOTEPATH/full-backup"

systemctl --user status home-backup.service after waking up from suspend:

Dec 05 03:30:45 <snip> systemd[1188]: Starting Backup home directory...
Dec 05 03:30:45 <snip> home-backup.sh[6170]: ssh: Could not resolve hostname <snip>: Name or service not known
Dec 05 03:30:45 <snip> home-backup.sh[6170]: rsync: connection unexpectedly closed (0 bytes received so far) [sender]
Dec 05 03:30:45 <snip> home-backup.sh[6170]: rsync error: unexplained error (code 255) at io.c(235) [sender=3.1.2]
Dec 05 03:30:45 <snip> systemd[1188]: home-backup.service: Main process exited, code=exited, status=255/n/a
Dec 05 03:30:45 <snip> systemd[1188]: Failed to start Backup home directory.
Dec 05 03:30:45 <snip> systemd[1188]: home-backup.service: Unit entered failed state.
Dec 05 03:30:45 <snip> systemd[1188]: home-backup.service: Failed with result 'exit-code'.

"There are no problems, only opportunities for solutions."

Offline

#2 2017-12-11 23:13:03

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,523
Website

Re: Using a systemd user timer to launch a network-dependent service

As it's your script, why not just check within the script, e.g.:

while ! ping -qc 1 8.8.8.8 >/dev/null 2>&1; do
   let count++
   [ count -gt 300 ] && exit
   sleep 1
done

# ... rsync stuff

Last edited by Trilby (2017-12-11 23:13:16)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2017-12-11 23:36:03

CrashLog
Member
From: Finland
Registered: 2013-01-13
Posts: 136

Re: Using a systemd user timer to launch a network-dependent service

That solved the issue when resuming from suspend. However, now when I try to log in after a reboot, if the timer activates as I'm logging in, the machine freezes for a while before asking me to log in again. Once logged in, there's something off about the system, such as gnome-keychain not opening on login, and being asked to authenticate for actions like

systemctl reboot

that I wouldn't need to authenticate for otherwise. I'm not sure what exactly goes wrong there.

Last edited by CrashLog (2017-12-11 23:37:24)


"There are no problems, only opportunities for solutions."

Offline

#4 2017-12-12 01:27:35

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,523
Website

Re: Using a systemd user timer to launch a network-dependent service

That's likely a coincidence.  Have you tried changing it back and rebooting again?

Looking back at your service file, the use of Type=oneshot isn't really appropriate, and it's possible the while loop just makes it run long enough for the delay of follow up services to be more noticably impacted.  In any case, Type should be 'simple'.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#5 2017-12-12 03:06:23

CrashLog
Member
From: Finland
Registered: 2013-01-13
Posts: 136

Re: Using a systemd user timer to launch a network-dependent service

Trilby wrote:

That's likely a coincidence.  Have you tried changing it back and rebooting again?

I undid the changes and the problem went away, but then it came back after I redid the changes again.

Trilby wrote:

Looking back at your service file, the use of Type=oneshot isn't really appropriate, and it's possible the while loop just makes it run long enough for the delay of follow up services to be more noticably impacted.  In any case, Type should be 'simple'.

Changing the service type seems to have fixed the problem. Now everything works after booting and waking from suspend as it should. Thanks!

Edit: I don't seem to be able to mark this solved. The page just loads for a while and then doesn't go anywhere. Hopefully a mod has better luck.

Last edited by CrashLog (2017-12-12 03:41:02)


"There are no problems, only opportunities for solutions."

Offline

Board footer

Powered by FluxBB