Like many other people, I have to fight with distractions a lot. The main distractors (at least when I work on my computer) are a few websites that rhyme with “litter”, “space crook” and “poo cube”.
Some time ago I decided that I need to take brutal action against them, and I put this in my /etc/hosts
file:
# begin distractors 127.0.0.1 twitter.com 127.0.0.1 facebook.com 127.0.0.1 www.facebook.com 127.0.0.1 youtube.com 127.0.0.1 www.youtube.com # end distractors
This turned out to be a bit too brutal, however – I sometimes really want to be able to use some of these terrible websites. I figured that it would be pretty cool if I could just turn them on for, say 10 minutes (or some other amount of time, decided in advance).
There is one obvious loophole in this idea: if I go to a “forbidden” site and don’t reload the page, I can still spend time there even after it gets disabled via the /etc/hosts
trick. Since all of them seem to use JS to fetch more content, this is not a big issue. Also, this is not intended as a safeguard I cannot disable (well, I can, of course, I have the root access), but rather as a nudge for me to stop wasting time.
So, let’s get coding. Enabling and disabling a section of /etc/hosts
is easy with sed
:
sed -i '/begin distractors/,/end distractors/ s/^# //' /etc/hosts # enable sed -i '/begin distractors/,/end distractors/ s/^/# /' /etc/hosts # disable
Now, here’s the trick. In order to do something after, say, 10 minutes, I can use the little-known at
command-line tool. (On my system, I had to explicitly enable the at
system service for it to work.) However, sudo
password times out after 5 minutes, so I have to make sure that at
is run as root. IOW, at sudo ...
wouldn’t work (most probably), but sudo at
is fine.
So, here is my enable-distractors.sh
script:
#!/bin/bash if [ "$1" == "" ] then TIMEOUT=10 minutes else TIMEOUT="$1" fi echo I will disable distractors again in "$1"! echo sudo sed -i '/begin distractors/,/end distractors/ s/^/# /' /etc/hosts echo sed -i \'/begin distractors/,/end distractors/ s/^# //\' /etc/hosts | sudo at now + "$TIMEOUT"
Before you copy and start using it, beware. This is a script which uses sudo
to run sed -i
on an important system configuration file in /etc
, written by a madman with moderate experience at shell scripts – so use it at your own risk, and definitely back up your /etc/hosts
before any experiments!