← Home

Auto blocking time consuming websites

@date=2025-06-05
@tags=productivity

Pasted image 20250707144404.png

I find that youtube, facebook, and reddit are a little too consuming of time and attention, but I also sometimes want to go lookup something on them.

To help with this, I block the sites using this /etc/hosts file on my mac:


# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.

# if you adjust these, reset the counter in obsidian.
127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost
127.0.0.1       reddit.com
127.0.0.1       www.reddit.com
127.0.0.1       youtube.com
127.0.0.1       www.youtube.com

If I want to look at something, I just edit the file, and comment out the site I want to see (put a # character at the beginning of the line).

But of course this will mean that until I come back and update the file, it will remain accessible to me.

To resolve this, I create a copy of /etc/hosts called /etc/hosts.source, and set it to copy over my /etc/hosts every 30 minutes. I do this using launchd, which is a way to schedule repeating jobs on a mac (a similar thing can be done with cron on linux).

In this tutorial I will show you how to do this.

First create this script: sudo nano /usr/local/bin/reset-hosts.sh

#!/bin/bash

# Copy source to actual hosts
cp /etc/hosts.source /etc/hosts
# Optional: flush DNS cache
dscacheutil -flushcache

Then make it executable:

sudo chmod +x /usr/local/bin/reset-hosts.sh

Make it able to be run without a password:

sudo EDITOR=nano visudo

Add this line at the end (replace yourusername with your actual macOS username):

yourusername ALL=(ALL) NOPASSWD: /usr/local/bin/reset-hosts.sh

Create a Launch Agent plist:

nano ~/Library/LaunchAgents/com.neverall.reset-hosts.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
 "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.neverall.reset-hosts</string>

    <key>ProgramArguments</key>
    <array>
        <string>sudo</string>
        <string>/usr/local/bin/reset-hosts.sh</string>
    </array>

    <key>StartInterval</key>
    <integer>1800</integer> <!-- Run every 30 minutes -->

    <key>RunAtLoad</key>
    <true/>

    <key>StandardErrorPath</key>
    <string>/tmp/reset-hosts.err</string>
    <key>StandardOutPath</key>
    <string>/tmp/reset-hosts.out</string>
</dict>
</plist>

Load it:

launchctl load ~/Library/LaunchAgents/com.neverall.reset-hosts.plist

Verify it: launchctl list | grep reset-hosts

You can also create an alias in your ~/.zprofile to manually run it:

alias reset-hosts='sudo /usr/local/bin/reset-hosts.sh'