Last active
July 10, 2025 03:46
-
-
Save prasann/fffef6208b462b9bf34cd2f758eefe79 to your computer and use it in GitHub Desktop.
Simple bash script that allows to block sites
This file contains hidden or 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 | |
| # Add websites to block below, separated by spaces and inside the parentheses: | |
| declare -a websitesToBlock=("reddit.com" "youtube.com" "x.com" "twitter.com" "linkedin.com") | |
| # Set block time in minutes (change this value as needed) | |
| BLOCK_TIME_MINUTES=30 | |
| unblock_sites() { | |
| sudo sed -i '' '/#start-Blocking/,/#stop-Blocking/d' /etc/hosts | |
| echo "Unblocking websites now!" | |
| echo "Thanks for using my script :)" | |
| exit 0 | |
| } | |
| if [ "$1" = "unblock" ]; then | |
| unblock_sites | |
| fi | |
| if [ "$1" = "unblock-site" ]; then | |
| SITE_TO_UNBLOCK="$2" | |
| if [ -z "$SITE_TO_UNBLOCK" ]; then | |
| echo "Usage: $0 unblock-site <site>" | |
| exit 1 | |
| fi | |
| sudo sed -i '' "/127.0.0.1 $SITE_TO_UNBLOCK/d" /etc/hosts | |
| sudo sed -i '' "/127.0.0.1 www.$SITE_TO_UNBLOCK/d" /etc/hosts | |
| sudo sed -i '' "/fe80::1%lo0 $SITE_TO_UNBLOCK/d" /etc/hosts | |
| sudo sed -i '' "/fe80::1%lo0 www.$SITE_TO_UNBLOCK/d" /etc/hosts | |
| echo "$SITE_TO_UNBLOCK has been unblocked." | |
| exit 0 | |
| fi | |
| if [ "$1" != "block" ]; then | |
| echo "Usage: $0 [block|unblock|unblock-site <site>]" | |
| exit 1 | |
| fi | |
| clear | |
| echo "This script will block distracting websites for $BLOCK_TIME_MINUTES minutes." | |
| timeToBlock=$((BLOCK_TIME_MINUTES * 60)) | |
| echo "To ensure a complete block, administrator rights will be needed!" | |
| echo "DO NOT modify the hosts file while this script is running or bad things will happen!" | |
| sudo cp /etc/hosts hosts.bak | |
| while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null & | |
| clear | |
| echo "Making a backup of the hosts file to the current directory..." | |
| echo "DO NOT CLOSE THE PROGRAM NOW OR THE WEBSITES WILL BE REMAINED BLOCKED." | |
| echo "Blocking Distractions..." | |
| sudo -- sh -c "echo '#start-Blocking' >> /etc/hosts" | |
| for i in "${websitesToBlock[@]}" | |
| do | |
| sudo -- sh -c "echo '127.0.0.1 $i' >> /etc/hosts" | |
| sudo -- sh -c "echo '127.0.0.1 www.$i' >> /etc/hosts" | |
| sudo -- sh -c "echo 'fe80::1%lo0 $i' >> /etc/hosts" | |
| sudo -- sh -c "echo 'fe80::1%lo0 www.$i' >> /etc/hosts" | |
| done | |
| sudo -- sh -c "echo '#stop-Blocking' >> /etc/hosts" | |
| echo "Successfully blocked distractions! It's time to work now..." | |
| echo "Websites will be unblocked automatically in $BLOCK_TIME_MINUTES minutes. You may close this terminal." | |
| nohup bash -c "sleep $timeToBlock; $(which sudo) sed -i '' '/#start-Blocking/,/#stop-Blocking/d' /etc/hosts" >/dev/null 2>&1 & | |
| exit 0 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
source credits: https://github.com/najeemk/distractions-blocker
added modifications to suit me on top of that.