7

I am using OSX 10.6 and it has cron built in. How can I write a cron job that will block my internet from 7pm to 7am every day?

Im sure this must be simple but I could only find one example online and it didn't work. It said to:

set /etc/hosts.allow file to 
be empty 
set hosts.deny file to
ALL : ALL

I did this and google still came up.

ᔕᖺᘎᕊ
  • 6,393

9 Answers9

10

After spending many hours trying to debug an issue with the job not running from root, I was able to get what you said working. Here are my exact steps:

In the terminal type:

$ sudo crontab -eu root

Then type:

0 19 * * * /sbin/ifconfig en0 down > /dev/null
0 7 * * * /sbin/ifconfig en0 up > /dev/null

and that does it! Credit to @Amardeep and @Stephenr for their contribution.

bad_coder
  • 649
5

The Parental Controls answer from Alan is probably the best way to do this. But just for posterity I'll describe why your hosts attempt didn't work and describe a cron type solution.

The hosts.allow (and its counterpart hosts.deny) are used to specify hosts that are allowed (or disallowed) to access your machine with certain services. What you're looking to do is the other way around which is why that didn't work.

The crontab entries that should disconnect then reconnect your network would be "something" like:

0 19 * * * /path/ifconfig eth0 down 1> /dev/null
0  7 * * * /path/ifconfig eth0 up   1> /dev/null

cron is implemented differently on many *nix systems and there are variations to the above format so consult your man page. Sorry I can't offer OS X specific detail. For security reasons some cron implementations will only set limited environment variables when executing the command so you should specify the full path by typing 'which ifconfig' at the shell prompt. You can debug it somewhat by removing the stdout redirection and many crons should email you the command's output or you could just redirect to a log file.

I'm not sure what the OS X ethernet device is called... it might be something different than eth0. Just type ifconfig and it should display the active device names. You might have to be root to even run ifconfig. You'll certainly need to be root to edit the crontab file.

3

In OS X, consider using the Parental Controls. It will do exactly what you're looking for.

Alan
  • 304
2

SelfControl does exactly this. Even rebooting won't stop it.

williamR
  • 131
1

Why use cron? Check this app out, much nicer and does a lot more: http://getconcentrating.com/

elsurudo
  • 111
1

Add the following to the crontab to delete the default route which will effectively disable any communication to the internet (but will still keeping network connectivity).

To disable (where a.b.c.d is your router):

route delete default a.b.c.d

TO re-enable:

route add default a.b.c.d
nolim1t
  • 111
1

Just unplug it or turn it off, why waste time looking for a solution that only requires 1 to 2 fingers. Why was this even posted lol

0

On a mac, the ethernet port is called en0 and the wifi port en1.

You can verify this by going to System Preferences -> Network then, selecting Ethernet, clicking Advanced then looking at the Ethernet tab - this will tell you the mac address. Do the same but select Airport to get the mac address of the wifi port.

Then in a terminal type ifconfig to get a list of network interfaces, that will show you the interface name for each interface (en0, en1, etc), and a load of other information, including the mac address which is labelled 'ether'.

Once you know what the names are of the interfaces, you can set up cron jobs to disable them as described by Amardeep. - use en0 instead of eth0, and repeat for the wifi port.

0

You may find Freedom a nice alternative if you’re lazy. It costs 10 u$s but it’s a nice way to say ok, no distractions.