1

In my /etc/hosts file, I have records like this:

127.0.0.1 www.youtube.com

To keep me from getting distracted. When I need something on one of the sites I have blocked, I add # to the beginning of the line. Running this command, I can make it so all of the lines can be uncommented to remove access again:

sudo sed -i '' 's/\#//g' /etc/hosts

To automatically remove access, I run sudo crontab -e and add this line

*/10 8-15 * * 1-5 sed -i '' 's/\#//g' /etc/hosts

I also tried with sudo crontab -e -u root

After saving it, mac asks for permission for terminal to admin the machine, which I grant, and I figure it should be all good, but it doesn't see that the command is ever run (the file doesn't get updated). Is there something I need to do to get it to update the file?

3
3

When you issue a command via cron, you need to specify the whole path, so instead, you should use /usr/bin/sed.

There are a couple other issues you should consider:

  • cron is deprecated and this should be done using launchd instead

  • Why edit the file in place? Simply save the original version of the hosts file (i.e. /etc/hosts.orig) and create a new hosts file for when you want to block YouTube (i.e. /etc/hosts.nodistractions). Just copy the appropriate file to /etc/hosts at the specified time interval.

    For example, to unblock everything:

      cp /etc/hosts.orig /etc/hosts
    

    To go into "no distraction mode":

      cp /etc/hosts.nodistractions /etc/hosts
    

The benefit of using this method is you can easily edit the file to add more hosts as your distractions evolve and you won't have to modify the code. Running this as a LaunchDameon executes this as root so there's no permission issues to really worry about. You won't have to contend with sudo or su or ensuring you're using the right user.

That said, there's an app called Focus ($20) that has a very rich feature set that will allow you to block not just websites, but apps as well. There's a "take a break" function that will allow you to release the restrictions for a configurable amount of time allowing you to take a break or as you described, get something when needed. The price point (IMO) is quite reasonable for what it provides.

Please note, I'm not affiliated with this app/company in any way nor do I personally use it. I just know of this product as a result of having to find a tool for parents of students - who were not focused on the task at hand - that gave them the ability to manage their distractions automatically; mainly due to location (parents at work, kids at home not doing homework).

1
  • I found sudo launchctl load ... and changing the permissions on /etc/hosts made it so I could update it every 10 minutes. Probably could do either one of these to get it working. Using cron just didn't work for this, so launchctl seems to be the way to go. – RobKohr Nov 20 '20 at 0:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .