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

6

1

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.

user242065

Posted 2010-06-12T03:31:29.927

Reputation:

2Unplug your cable, or simply shut down the machine man. It's not that hard. – user229044 – 2010-06-12T03:32:57.227

1This can also be a setting on your router. – Zurahn – 2010-06-12T03:34:45.417

6It 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. – None – 2010-06-12T04:10:37.940

Answers

8

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

user242065

Posted 2010-06-12T03:31:29.927

Reputation:

4

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.

Amardeep

Posted 2010-06-12T03:31:29.927

Reputation:

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 http://stackoverflow.com/questions/3027574/help-with-running-crontab-from-root

– None – 2010-06-12T05:14:08.053

cron implementation does vary greatly between systems so I've revised the answer with a few details that might be helpful. – None – 2010-06-12T12:55:18.427

3

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

Alan

Posted 2010-06-12T03:31:29.927

Reputation: 304

3Footnote: Some self-discipline required. Parent not included. – Jim Lewis – 2010-06-12T03:40:40.490

For $25 an hour I'll be his parent :D – Alan – 2010-06-12T03:57:36.797

This doesn't quite work it limits computer access, I only want to limit internet access. – None – 2010-06-12T04:17:00.637

1Ah, well your question title said you wanted to get some sleep. – Alan – 2010-06-12T05:15:19.310

2

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

williamR

Posted 2010-06-12T03:31:29.927

Reputation: 111

1

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

elsurudo

Posted 2010-06-12T03:31:29.927

Reputation: 111

1this doesn't do what I want and costs money – None – 2010-06-12T05:14:32.467

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

Posted 2010-06-12T03:31:29.927

Reputation: 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

int64_t

Posted 2010-06-12T03:31:29.927

Reputation:

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.

stephenr

Posted 2010-06-12T03:31:29.927

Reputation:

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.

Martin Marconcini

Posted 2010-06-12T03:31:29.927

Reputation: 1 409