Skip to content

Instantly share code, notes, and snippets.

@iandark
Created September 9, 2021 05:50
Show Gist options
  • Select an option

  • Save iandark/951d4edb3f210a28aca838bbe5d67203 to your computer and use it in GitHub Desktop.

Select an option

Save iandark/951d4edb3f210a28aca838bbe5d67203 to your computer and use it in GitHub Desktop.
Check if internet is on and restart VPN if is down
## First you need to create a job in crontab, you must execute this with 'sudo' first to guarantee the root user routine
## $ sudo crontab -e
## Append a line at the end of the file, here i use a every 1 hour schedulle execution with the path to script.
## 0 */1 * * * /home/USERNAME/internetIsOn.sh
## Optional: if you want to create a simple log to count how many hours you lost your connection follow these steps
## 1 - Create a log directory
## $ sudo mkdir -p /var/log/internetIsOn
## 2 - Change ownership of directory to make this available to your user anytime
## $ sudo chown -R USER:USERGROUP /var/log/internetIsOn
## 3 - Change permission to only your user can write and user group can read
## $ sudo chmod -R 750 /var/log/internetIsOn
## 4 - Uncomment log lines in the script below
## Now the script;
#!/bin/bash
wget -q --tries=10 --timeout=20 --spider http://google.com
if [[ $? -eq 0 ]]; then
## date +"%Y-%m-%d %H:%M - online\n" >> /var/log/internetIsOn/daemon.log
else
## date +"%Y-%m-%d %H:%M - OFFLINE\n" >> /var/log/internetIsOn/daemon.log
sudo systemctl restart VPN@SERVICE.service
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment