Skip to content

Instantly share code, notes, and snippets.

@alkimiadev
Created January 22, 2026 19:54
Show Gist options
  • Select an option

  • Save alkimiadev/aa1de1fbce8d9bd11eb05da3cbef6f50 to your computer and use it in GitHub Desktop.

Select an option

Save alkimiadev/aa1de1fbce8d9bd11eb05da3cbef6f50 to your computer and use it in GitHub Desktop.
silly script to block X and meant to be used with a cron to limit usage
#!/bin/bash
# The domains we want to block
DOMAINS="x.com www.x.com twitter.com www.twitter.com"
# Where we point them (Localhost)
REDIRECT="127.0.0.1"
# The hosts file path
HOSTS="/etc/hosts"
# A unique comment to identify our entry
MARKER="# X_BLOCK_CONTROL"
# Function to block X
block_x() {
# Check if we've already added it to prevent duplicates
if grep -q "$MARKER" "$HOSTS"; then
echo "X.com is already blocked."
else
echo "Blocking X.com..."
echo -e "$REDIRECT $DOMAINS $MARKER" >> "$HOSTS"
flush_dns
fi
}
# Function to unblock X
unblock_x() {
echo "Unblocking X.com..."
# Remove any line containing the marker
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS version of sed
sed -i '' "/$MARKER/d" "$HOSTS"
else
# Linux version of sed
sed -i "/$MARKER/d" "$HOSTS"
fi
flush_dns
}
# Function to flush DNS cache so changes take effect immediately
flush_dns() {
echo "Flushing DNS cache..."
if [[ "$OSTYPE" == "darwin"* ]]; then
sudo dscacheutil -flushcache && sudo killall -HUP mDNSResponder
else
# Many Linux distros use systemd-resolved
if command -v systemd-resolve &> /dev/null; then
sudo systemd-resolve --flush-caches
elif command -v service &> /dev/null; then
# Try generic restart of dns service
sudo service dns-clean restart &> /dev/null
fi
fi
}
# Main logic
case "$1" in
block)
block_x
;;
unblock)
unblock_x
;;
*)
echo "Usage: $0 {block|unblock}"
exit 1
;;
esac
@alkimiadev
Copy link
Author

alkimiadev commented Jan 22, 2026

This can be used by

sudo ./x_control.sh block   # Try going to x.com now. It should fail.
sudo ./x_control.sh unblock # It should work again.

I'm personally using it with a cron job that will only unlock it between 6 and 7 pm daily

sudo crontab -e
# Unblock X.com at 6:00 PM daily
0 18 * * * /path/to/your/x_control.sh unblock

# Block X.com at 7:00 PM daily
0 19 * * * /path/to/your/x_control.sh block

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment