views:

75

answers:

4

hi, I want to schedule access to some website for a limited period of time say for 1 hour every day. How can i do that using cron job in linux. or can i do that using linux squid server?.

+1  A: 

It matters what you want to happen when the site isn't accessible. You could simply only have Apache start for that time. You could use iptables (or another firewall) to not allow connections on port 80. You could also just edit configuration file and deny all with Apache on that website.

Give a bit more detail on what you want to do.

William
Also, port 443 for SSL.
William
Or you could do it code-side. Let the app decide if the time is right and redirect to a "Sorry, we're closed" page otherwise.
echo
Very good point. I was actually wondering why he posted on here instead of Serverfault for this. Although he did say "cron" which makes it a bit confusing, although he might just be confused on what he wants. Good point :)
William
A: 

I am not 100% sure what you are asking - are you talking about running a httpd (Apache) that is hosted on your server and allowing someone to check the site at a particular time maybe a demo site for someone or are you an administrator who wants to impose restrictions on web surfing within your network? If your answer is yes to the second question, I think this question should be moved across to superuser.com.

I guess William's answer combined with cron, one could schedule to refuse packets (incoming/outgoing) within iptables for port 80/443, at all times other then the times specified in the cron job. You would have to make sure that this does work as you will have to clear the iptables each time so you would need to save the iptables configuration dump to a file somewhere.

As for Squid, you can check here.

Hope this helps, Best regards, Tom.

tommieb75
You don't have to clear iptables! Just iptables -A INPUT ... -j ALLOW to append an input rule, then iptables -D INPUT <the same ...> -j ALLOW to delete it later. You can delete by numeric index in the list, but it's safest to delete the same rule you added.
Peter Cordes
+2  A: 

In /etc/crontab:

0 18    * * *   root    /etc/init.d/apache start
0 19    * * *   root    /etc/init.d/apache stop

The apache service will start at 18:00 every day, and stop at 19:00 every day. Adjust as needed for your distro's init script structure.

Peter Cordes
+1  A: 

if you do go the iptables route (which may not be the best way to do this), you can use the "time" module in iptables, as in:

iptables -A -m state --state NEW,ESTABLISHED -p tcp --dport 80 -d -m time --timestart 1800 --timestop 1900 -j ACCEPT

Peter Carrero