Skip to content

Instantly share code, notes, and snippets.

@samullen
Last active January 30, 2019 18:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samullen/6497129 to your computer and use it in GitHub Desktop.
Save samullen/6497129 to your computer and use it in GitHub Desktop.
The two most productivity increasing Bash functions ever written.
function worktime {
echo "# WORKTIME" | sudo tee -a /etc/hosts > /dev/null
while read -r line; do
echo "127.0.0.1 ${line}"
done < $HOME/.blocked_sites | sudo tee -a /etc/hosts > /dev/null
}
function slacktime {
flag=0
while read -r line; do
[[ $line =~ "# WORKTIME" ]] && flag=1
[[ $flag -eq 1 ]] && continue
echo $line
done < /etc/hosts > /tmp/hosts
sudo cp /tmp/hosts /etc/hosts
}
@samullen
Copy link
Author

samullen commented Sep 9, 2013

Two Bash functions to help with productivity:

  • worktime appends the contents of $HOME/.blocked_sites to the end of /etc/hosts.
  • slacktime removes the contents added by worktime

the contents of .blocked_sites just need to be the domains you want to avoid. One per line.

Example:

twitter.com 
www.youtube.com 
youtube.com 
facebook.com

Attempting to go to sites included in .blocked_sites once worktime has been called will send the user to localhost until /etc/hosts has had those lines removed either manually or by calling slacktime

@shime
Copy link

shime commented Dec 20, 2013

@samullen very helpful, thanks. what's the setup needed for the landing page you describe on your blog post?

@mhutter
Copy link

mhutter commented May 15, 2014

Cool stuff! I also added a block and an unblock function (I am super lazy!)

Also, add this to your sudoers-file to avoid typing your password every time you switck to workmode/slackmode:

username ALL=(root) NOPASSWD: /usr/bin/tee -a /etc/hosts
username ALL=(root) NOPASSWD: /bin/cp /tmp/hosts /etc/hosts

@shime You have to set up a local Webserver listening on Port 80, serving said landing page.

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