Skip to content

Instantly share code, notes, and snippets.

@sprytnyk
Created April 9, 2020 12:38
Show Gist options
  • Save sprytnyk/d29493676aa883f6b5ee656008e4d377 to your computer and use it in GitHub Desktop.
Save sprytnyk/d29493676aa883f6b5ee656008e4d377 to your computer and use it in GitHub Desktop.
A simple script to turn on/off systemd-resolved.service from /etc/openvpn/update-resolv-conf.
#!/usr/bin/env bash
STATUS="$(sudo systemctl status systemd-resolved.service)"
# Stops the service if systemd-resolv.service is running.
if echo "${STATUS}" | grep -q "running"
then
sudo systemctl stop systemd-resolved.service > /dev/null 2>&1
if sudo systemctl status systemd-resolved.service | grep -q "inactive"
then
echo -e "\e[44mThe systemd-resolved.service has been stopped.\e[0m" && exit 0
else
echo -e "\e[41mThe service is still running.\e[0m" && exit 1
fi
fi
# Starts the service if systemd-resolv.service is not running.
if echo "${STATUS}" | grep -q "inactive"
then
sudo systemctl start systemd-resolved.service > /dev/null 2>&1
if sudo systemctl status systemd-resolved.service | grep -q "running"
then
echo -e "\e[42mThe systemd-resolved.service has been launched.\e[0m" && exit 0
else
echo -e "\e[41mThe service is still inactive.\e[0m" && exit 1
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment