So here’s the script I started:
[Unit]
Description=Connecting VPN via PIA
Wants=piavpn.service
After=piavpn.service
StartLimitIntervalSec=0
[Service]
Type=forking
ExecStartPre=/bin/bash -c 'while [ "$(systemctl is-active piavpn.service)" != "active" ]; do sleep 1; done'
ExecStart=/usr/bin/bash -c '/opt/piavpn/bin/piactl connect; \
while true; do \
connection_state=$(/opt/piavpn/bin/piactl get connectionstate); \
if [ "$connection_state" == "Connected" ]; then \
echo "PIA - Connected"; break; \
fi; \
echo "Waiting for connection..."; /usr/bin/sleep 5; \
done; \
while true; do if [ "$(/opt/piavpn/bin/piactl get connectionstate)" == "Connected" ]; then echo "PIA - Still Connected"; sleep 15; else echo "PIA - Disconnected; broken"; exit 1; fi; done'
ExecStop=/usr/bin/bash -c '/opt/piavpn/bin/piactl disconnect; while true; do if [ "$(/opt/piavpn/bin/piactl get connectionstate)" == "Disconnected" ]; then echo "PIA - Disconnected"; exit 0; else echo "PIA - Still Connected"; fi; sleep 5; done'
Restart=always
#RemainAfterExit=yes
[Install]
WantedBy=default.target
While the basic version was able to start properly, I’m unable to keep it checking and restart if it disconnects.
$ systemctl --user status pia_activate.service
● pia_activate.service - Connecting VPN via PIA
Loaded: loaded (/home/.tor/.config/systemd/user/pia_activate.service; enabled; vendor preset: enabled)
Active: activating (start) since Fri 2023-09-22 06:32:20 UTC; 1min 1s ago
Process: 31744 ExecStartPre=/bin/bash -c while [ "$(systemctl is-active piavpn.service)" != "active" ]; do sleep 1; done (code=exited, status=0/SUCCESS)
Cntrl PID: 31746 (bash)
Tasks: 2 (limit: 9231)
Memory: 544.0K
CPU: 341ms
CGroup: /user.slice/user-1002.slice/user@1002.service/app.slice/pia_activate.service
├─31746 /usr/bin/bash -c "/opt/piavpn/bin/piactl connect; \twhile true; do \tconnection_state=\$(/opt/piavpn/bin/piactl get connectionstate); \tif [ \"\$connection_state\" == \"Connected\" ]; then \t\techo \"PIA - Connected\"; break; \tfi; \techo \"Waiting for connection...\"; /usr/bin/sleep 5; \tdone; \twhile >
└─31815 sleep 15
Sep 22 06:32:20 evie systemd[761]: Starting Connecting VPN via PIA...
Sep 22 06:32:20 evie bash[31746]: PIA - Connected
Sep 22 06:32:20 evie bash[31746]: PIA - Still Connected
Sep 22 06:32:36 evie bash[31746]: PIA - Still Connected
Sep 22 06:32:51 evie bash[31746]: PIA - Still Connected
Sep 22 06:33:06 evie bash[31746]: PIA - Still Connected
Sep 22 06:33:21 evie bash[31746]: PIA - Still Connected
This line:
while true; do if [ "$(/opt/piavpn/bin/piactl get connectionstate)" == "Connected" ]; then echo "PIA - Still Connected"; sleep 15; else echo "PIA - Disconnected; broken"; exit 1; fi; done'
I originally added on ExecStartPost, and it’d cause Active: activating (start-post)
instead of activating (start)
.
I’m not as experienced creating custom systemd scripts, and any help would be greatly appreciated.
In addition, would it be possible to create a systemd script for another service that’ll stop the service if this one stops working and will restart if it’s successfully back up?