Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

I did this and google still came up.

share|improve this question

migrated from Jun 14 '10 at 0:37

This question came from our site for professional and enthusiast programmers.

Unplug your cable, or simply shut down the machine man. It's not that hard. – meagar Jun 12 '10 at 3:32
This can also be a setting on your router. – Zurahn Jun 12 '10 at 3:34
Get a girlfriend. – Sam Miller Jun 12 '10 at 3:37
It is impossible. If you have admin access to computer, you can't block yourself out - no matter what kind of block you install, you will be able to remove it yourself. And you will do it, thinking something like "just five more minutes". You'll have to improve self-control. Or get a girlfriend. Either way will work better than cron job. – SigTerm Jun 12 '10 at 4:10

Thank you Armardeep, 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! Thank you Amardeep and Stephenr

share|improve this answer

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.

share|improve this answer
Yes this is close to the answer I am looking for but I cant get it to work. I re asked the question with the info you gave me here… – Anonymous Jun 12 '10 at 5:14
cron implementation does vary greatly between systems so I've revised the answer with a few details that might be helpful. – Amardeep Jun 12 '10 at 12:55

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

share|improve this answer
Footnote: Some self-discipline required. Parent not included. – Jim Lewis Jun 12 '10 at 3:40
For $25 an hour I'll be his parent :D – Alan Jun 12 '10 at 3:57
This doesn't quite work it limits computer access, I only want to limit internet access. – Anonymous Jun 12 '10 at 4:17
Ah, well your question title said you wanted to get some sleep. – Alan Jun 12 '10 at 5:15

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

share|improve this answer
thanks a lot, great program! – Renaud Jul 6 '12 at 8:32

Why use cron? Check this app out, much nicer and does a lot more:

share|improve this answer
this doesn't do what I want and costs money – Anonymous Jun 12 '10 at 5:14

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
share|improve this answer

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

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

You must log in to answer this question.