You are not logged in.

#1 2015-11-01 20:14:42

emacsomancer
Member
Registered: 2014-09-20
Posts: 211

[solved] Resume hook for systemd to restart VPN

Since when my laptop sleeps, it shuts off the network, my VPN application isn't happy on wake, so I need to exit it down on sleep and restart on wake.

I've got the sleep part worked out, but resuming it is difficult.

Here's my /etc/systemd/system/restore@.service file:

[Unit] 
Description=User resume actions
After=suspend.target

[Service]
User=%I
Type=forking
ExecStart=/usr/bin/sh /home/emacsomancer/pia.sh

[Install]
WantedBy=suspend.target

This works, up to a point. The problem is that `pia.sh` needs to be run as user, but it ends up forking an `openvpn` process controlled by root.

So it work fine to run in command line: `/usr/bin/sh /home/emacsomancer/pia.sh`. With `ps aux` I can see all of the user-related pia-processes, plus the root openvpn-process.

But as a systemd service, setup as above, only the user-level pia-processes start, but not the root openvpn-process.

Last edited by emacsomancer (2015-11-01 21:34:23)

Offline

#2 2015-11-01 21:33:29

emacsomancer
Member
Registered: 2014-09-20
Posts: 211

Re: [solved] Resume hook for systemd to restart VPN

Figured it out. Had to do with setting the environment variable.

So this works:

[Unit] 
Description=User resume actions
After=suspend.target

[Service]
Environment=DISPLAY=:0
User=%I
Type=forking
ExecStart=/usr/bin/sh /home/emacsomancer/pia.sh

[Install]
WantedBy=suspend.target

In fact this works better (the above doesn't seem to work when the PIA VPN was running before suspend).

/etc/systemd/system/vpn@.service

[Unit]
Description=Restart PIA VPN
Requires=network.target
After=network.target

[Service]
Environment=DISPLAY=:0
User=%I
Type=forking
ExecStart=/usr/bin/sh /home/USERNAME/pia.sh
Restart=always
RestartSec=5

[Install]
WantedBy=network.target

/etc/systemd/system/suspend@.service

[Unit]
Description=User suspend actions
Before=suspend.target

[Service]
User=%I
Environment=DISPLAY=:0
Type=oneshot
ExecStart=-/usr/bin/pkill -f pia
ExecStartPost=/usr/bin/sleep 5

[Install]
WantedBy=sleep.target

Last edited by emacsomancer (2015-11-01 23:45:49)

Offline

Board footer

Powered by FluxBB