-
Create a script file
nano autoUpdateHostsOnSystemRestart.sh
and save it in your desired directory. (Here, I've saved it in my/home/user
directory) -
Paste the below script content in the file.
-
Remember to make your script executable using the command
chmod +x autoUpdateHostsOnSystemRestart.sh
. Then you can run your script with./autoUpdateHostsOnSystemRestart.sh
. -
Create a new service file in /etc/systemd/system/ with a .service extension, for example myscript.service. You can use
sudo nano /etc/systemd/system/updateHosts.service
to open a new file in the nano text editor. -
Add the following content:
[Unit] Description=Update /etc/hosts on unlock [Service] Type=oneshot ExecStart=sudo /home/user/autoUpdateHostsOnSystemRestart.sh [Install] WantedBy=multi-user.target
-
Save and close the file (in nano, you can do this by pressing Ctrl+X, then Y to confirm saving).
-
Now you need to reload the systemd manager configuration with:
sudo systemctl daemon-reload
. -
Enable your service to be run at startup:
sudo systemctl enable updateHosts.service
-
You can start your service immediately with:
sudo systemctl start updateHosts.service
-
You can check the status of your service anytime with:
sudo systemctl status updateHosts.service
Last active
October 25, 2023 18:36
-
-
Save imshaiknasir/5b7852754bfade40710a0512257679b2 to your computer and use it in GitHub Desktop.
this script is a simple and effective way to block ads on a Linux system
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
{ | |
# wait until network-manager back in online status | |
while ! ping -q -c 1 -W 1 google.com >/dev/null; do | |
echo "The network is down" | |
sleep 30 | |
done | |
echo "The network is UP" | |
if ping -q -c 1 -W 1 google.com >/dev/null; then | |
echo "The network is up" | |
# Path to the hosts file | |
HOSTS_FILE="/etc/hosts" | |
# URL of the GitHub ad block list | |
AD_BLOCK_LIST_URL="http://sbc.io/hosts/alternates/fakenews-gambling-porn/hosts" | |
# Backup the existing hosts file | |
sudo cp "$HOSTS_FILE" "$HOSTS_FILE.bak" | |
# Download the latest ad block list and store it in a temporary file | |
TEMP_FILE=$(mktemp) | |
curl -s "$AD_BLOCK_LIST_URL" >"$TEMP_FILE" | |
# Combine the current hosts file and the new ad block list, remove duplicates, and sort | |
cat "$HOSTS_FILE" "$TEMP_FILE" | sudo sort -u >"$HOSTS_FILE" | |
# Clean up the temporary file and echo it | |
rm -f "$TEMP_FILE" && echo "Updated $HOSTS_FILE" | |
# Restart networking to apply the changes (may require root/sudo access) | |
sudo systemctl restart NetworkManager.service | |
# wait until network-manager back in online status | |
while ! ping -q -c 1 -W 1 google.com >/dev/null; do | |
echo "The network is down" | |
sleep 5 | |
done | |
echo "The network is UP" | |
else | |
echo "The network is down" | |
exit 1 | |
fi | |
} >/home/user/updateAdHosts.log 2>&1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment