Skip to content

Instantly share code, notes, and snippets.

@tcard
Last active January 12, 2021 07:50
Show Gist options
  • Save tcard/08f3db554a529e314875dc4b26377b52 to your computer and use it in GitHub Desktop.
Save tcard/08f3db554a529e314875dc4b26377b52 to your computer and use it in GitHub Desktop.
Poor man's block websites script

First, let's make the script executable:

chmod +x block-etc-hosts.sh

To start blocking:

sudo block-etc-hosts.sh block

To stop blocking:

sudo block-etc-hosts.sh unblock

To stop blocking for a while (in minutes):

sudo block-etc-hosts.sh 10

There's also a couple launch daemons for macOS, to automatically block during workdays. Put them in /Library/LaunchDaemons, make sure you change /path/to/block-etc-hosts.sh and the StartCalendarInterval property to suit your preferences, then:

sudo chown root:wheel /Library/LaunchDaemons/*-etc-hosts.plist
sudo launchctl load /Library/LaunchDaemons/block-etc-hosts.plist
sudo launchctl load /Library/LaunchDaemons/unblock-etc-hosts.plist

The equivalent cronjobs would be:

30 8 * * MON-FRI /path/to/block-etc-hosts.sh block
30 18 * * MON-FRI /path/to/block-etc-hosts.sh unblock

Launch daemons have the advantage that they eventually run, even if the computer is off or suspended at the scheduled time.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>block-etc-hosts</string>
<key>UserName</key>
<string>root</string>
<key>ProgramArguments</key>
<array>
<string>/path/to/block-etc-hosts.sh</string>
<string>block</string>
</array>
<key>StartCalendarInterval</key>
<array>
<dict>
<key>Minute</key>
<integer>30</integer>
<key>Hour</key>
<integer>8</integer>
<key>Weekday</key>
<integer>1</integer>
</dict>
<dict>
<key>Minute</key>
<integer>30</integer>
<key>Hour</key>
<integer>8</integer>
<key>Weekday</key>
<integer>2</integer>
</dict>
<dict>
<key>Minute</key>
<integer>30</integer>
<key>Hour</key>
<integer>8</integer>
<key>Weekday</key>
<integer>3</integer>
</dict>
<dict>
<key>Minute</key>
<integer>30</integer>
<key>Hour</key>
<integer>8</integer>
<key>Weekday</key>
<integer>4</integer>
</dict>
<dict>
<key>Minute</key>
<integer>30</integer>
<key>Hour</key>
<integer>8</integer>
<key>Weekday</key>
<integer>5</integer>
</dict>
</array>
</dict>
</plist>
#!/usr/bin/env bash
base="$(cat /etc/hosts | grep -v '# BLOCK #')"
function block {
cat > /etc/hosts <<EOF
$base
127.0.0.1 twitter.com # BLOCK #
127.0.0.1 mobile.twitter.com # BLOCK #
127.0.0.1 reddit.com # BLOCK #
EOF
}
function unblock {
cat > /etc/hosts <<EOF
$base
EOF
}
case "$1" in
'block' )
block
;;
'unblock' )
unblock
;;
'toggle' )
if (cat /etc/hosts | grep '# BLOCK #' > /dev/null); then
unblock
else
block
fi
;;
[0-9]* )
unblock
( sleep "$(expr "$1" '*' 60)"; block ) &
;;
esac
Copyright (c) 2021 Toni Cárdenas
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>unblock-etc-hosts</string>
<key>UserName</key>
<string>root</string>
<key>ProgramArguments</key>
<array>
<string>/path/to/block-etc-hosts.sh</string>
<string>unblock</string>
</array>
<key>StartCalendarInterval</key>
<array>
<dict>
<key>Minute</key>
<integer>30</integer>
<key>Hour</key>
<integer>18</integer>
<key>Weekday</key>
<integer>1</integer>
</dict>
<dict>
<key>Minute</key>
<integer>30</integer>
<key>Hour</key>
<integer>18</integer>
<key>Weekday</key>
<integer>2</integer>
</dict>
<dict>
<key>Minute</key>
<integer>30</integer>
<key>Hour</key>
<integer>18</integer>
<key>Weekday</key>
<integer>3</integer>
</dict>
<dict>
<key>Minute</key>
<integer>30</integer>
<key>Hour</key>
<integer>18</integer>
<key>Weekday</key>
<integer>4</integer>
</dict>
<dict>
<key>Minute</key>
<integer>30</integer>
<key>Hour</key>
<integer>18</integer>
<key>Weekday</key>
<integer>5</integer>
</dict>
</array>
</dict>
</plist>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment