How can I write a cron job that will block my internet from 7pm to 7am? (So I can get some sleep)

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.


Solution 1:

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.

Solution 2:

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.