Skip to content

Instantly share code, notes, and snippets.

@Mic92
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mic92/3f25e1b99997e845e148 to your computer and use it in GitHub Desktop.
Save Mic92/3f25e1b99997e845e148 to your computer and use it in GitHub Desktop.
Restart openvpn connection after systemd suspend
#!/bin/sh
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
if ! which systemctl >/dev/null; then
echo "No systemctl found, systemd is required for this files" 1>&2
exit 1
fi
echo "install /etc/systemd/system/openvpn-sleep.service"
mkdir -p "/etc/systemd/system/"
cat > /etc/systemd/system/openvpn-sleep.service <<EOF
[Unit]
Description=Suspend openvpn
Before=sleep.target
StopWhenUnneeded=yes
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/true
ExecStop=/usr/bin/bash -c "while sleep 0.1; do ping6 -W1 -c3 2001:4860:4860::8888 || ping -W1 -c3 8.8.8.8 && break; done && pkill -USR1 openvpn"
[Install]
WantedBy=sleep.target
EOF
systemctl daemon-reload
systemctl enable openvpn-sleep.service
echo "Finished"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment