You are not logged in.

#1 2019-09-02 04:46:51

GoblinMendicant
Member
Registered: 2019-09-02
Posts: 15

Systemd service that runs after wake up from sleep and network connect

Quick overview: I wrote a systemd service that nearly does what I want, but not quite. I want it to run a script (the script stops and starts wireguard) after two requirements are met: waking up from sleep & connected to the network. It runs after waking up from sleep, but isn't waiting for the network to connect.

Background: I am on Arch, but back when I was on Manjaro, I used this tutorial to set up and connect to/from wireguard. To start/stop, you use wg-quick, a la

wg-quick up mullvad-blahblah

On Manjaro, if I was connected to wireguard prior to putting my laptop to sleep, and then I later woke it up from sleep, the wireguard connection was still present, and the network/internet connection worked. Ok, so now in Arch, I followed this same tutorial since it seems so simple and straightforward. Only difference is now when I wake up from sleep, it seems that wireguard is connecting before the network connection is established, causing the internet connection to not work. I don't know why this setup worked fine on manjaro (only obvious difference is that I was using lightdm, and now I'm using sddm).

First off, is anyone knows the solution to this, then that will immediately solve my problem.

But I don't, so I made a systemd service that upon wake disconnects and reconnects to wireguard. It mostly works. The problem is that it starts prior to the network connection being established, even though as far as my knowledge of systemd goes, it should be waiting! The hacky fix is to add a 10 second sleep to my script between reconnecting. This works pretty well. But it feels like a dumb solution.

I'm using kde/plasma, networkmanager, and sddm

Systemd service

[Unit]
Description=Run restart_wireguard
Requires=network.target
After=network.target suspend.target hibernate.target hybrid-sleep.target

[Service]
User=root
Type=oneshot
ExecStart=/usr/local/bin/restart_wireguard.sh

[Install]
WantedBy=suspend.target hibernate.target hybrid-sleep.target

restart_wireguard.sh

#!/bin/sh

wg-quick down mullvad-us2
wg-quick up mullvad-us2

1. Can you see anything wrong with the above service that's making it not wait until the network is established? I also tried "Requires=network-online.target" instead of "Requires=network.target", and it didn't help (although I didn't add it to "After" as well). I have a feeling I'm misunderstanding the definitions of Require and After.

2. Should I not be going the systemd route here? Here's a possible alternative solution

Thanks!

Offline

#2 2019-09-02 07:26:06

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,427

Re: Systemd service that runs after wake up from sleep and network connect

You will want to wait on network-online.target. To have that properly invoked you must enable NetworkManager-wait-online.service. network.target is invoked when the network hardware becomes available, which is not yet related to actual connection. You will also want to add it to After in addition. The logic here is: Requires= tells systemd to activate this unit once the specified unit is started, this does not yet define order and will likely to a parallel execution. Additionally specifying in After= tells systemd to wait until the specified unit is done (this does analogous not define activation requirements, just ordering) hence you need both:

Offline

#3 2019-09-02 21:58:55

GoblinMendicant
Member
Registered: 2019-09-02
Posts: 15

Re: Systemd service that runs after wake up from sleep and network connect

Thanks! This still didn't fix the issue. I already had NetworkManager-wait-online.service enabled, and so I put "network-online.target" in both Require and After, and it still seems to immediately start the service.

At this point I think I'm going to focus more on why exactly wireguard breaks the network connection after waking up. It's really strange to me that this happens on arch and not manjaro. I've tried different kernels, and both wireguard-dkms and wireguard-arch.

Offline

#4 2019-09-02 23:51:03

loqs
Member
Registered: 2014-03-06
Posts: 17,196

Re: Systemd service that runs after wake up from sleep and network connect

I suspect systemd considers conditions including network-online.target and network.target met from the initial boot and that is not altered by hibernation.

Offline

#5 2019-09-03 07:42:36

seth
Member
Registered: 2012-09-03
Posts: 49,992

Re: Systemd service that runs after wake up from sleep and network connect

You could also wait for the network in the restart script by pinging the relevant domain/IP until success.

Offline

#6 2020-01-08 18:02:48

sylphio
Member
Registered: 2020-01-08
Posts: 2

Re: Systemd service that runs after wake up from sleep and network connect

Hi !
I am facing the same issue, as far as I can tell. Have you found the cause of that connection loss, or a way around it? I have not found much relevant information so far (my web searches led me here).

Offline

#7 2020-01-08 18:40:37

GoblinMendicant
Member
Registered: 2019-09-02
Posts: 15

Re: Systemd service that runs after wake up from sleep and network connect

My solution was to add a

sleep 10

to the systemctl process, so that wireguard isn't enabled until after the network is connected. And this seems to work for me.

I can share the full code for the process later, if you need it.

Offline

#8 2020-01-10 13:27:20

sylphio
Member
Registered: 2020-01-08
Posts: 2

Re: Systemd service that runs after wake up from sleep and network connect

That seems to work fine for me. I am actually pleasantly surprising to see my issue fixed by such a light workaround.
Thanks!

Offline

Board footer

Powered by FluxBB