Register
It is currently Fri Nov 28, 2014 1:17 pm

cron script to alter hosts by time...


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Mon Feb 07, 2011 7:45 pm   

Joined: Mon Feb 07, 2011 7:37 pm
Posts: 1
I need a script to disable a list of sites at 7am, then enable them at 7pm.
Facebook and MySpace for a good start... since I would be much more productive. ;;)

I know I can change the hosts file like so to stop Facebook for example:
127.0.0.1 facebook.com #now it points to my personal server ;) 0.0.0.0 might work too ?

but to have it do this automatically would be great. Only during the week would be a bonus.

It has to be pretty easy but I don't know how to set it up. thank you very much in advance.


Top
 Profile  
 PostPosted: Tue Feb 08, 2011 10:34 am   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Hi poetfreak!

The script itself should be easy enough. I'd probably just use something like 'echo' to append those lines to the end of the hosts file, and 'sed' to delete them. What do you have so far? Do you know how to format the crontab? You can edit it with 'crontab -e'

I hope this helps!
-J


Top
 Profile YIM  
 PostPosted: Sat Mar 05, 2011 9:38 pm   

Joined: Mon May 31, 2010 7:40 pm
Posts: 25
Another drive by post that I can gain valuable experience I guess.

So in writing this script I noticed that when I remove the lines with sed it leaves blank lines where the text is removed.

An example after running the script and sites are removed I still see the blank lines left from where the sites were removed.

Code:
# Hosts File
# Before sed
127.0.0.1 facebook.com
127.0.0.1 myspace.com
~
~
~


After sed removes these lines and vim displays the file as follows:

Code:
# hosts file
# after sed

~
~
~


I tried various sed commands in my script such as

sed -i /^$/d' $HOSTS

and

sed -i / *$/d' $HOSTS

this is really bothering me

:)


Last edited by canit0 on Sat Mar 05, 2011 9:54 pm, edited 1 time in total.

Top
 Profile  
 PostPosted: Sat Mar 05, 2011 9:41 pm   

Joined: Mon May 31, 2010 7:40 pm
Posts: 25
Here is the script

Code:
#!/bin/bash

HOME=~/scripts/bash
HOSTS=$HOME/hosts
DATE="/bin/date +%H"
LOGFILE=$HOME/time.log
SITES=$HOME/sites.txt
SDATE="/bin/date"
AM="7"

set-sites () {
    cat $SITES >> $HOSTS
}

rem-sites () {
    sed -i '/delete/g' $HOSTS
}

if [[ ${DATE} == $AM ]]; then
    set-sites;
     echo $SDATE: Sites added >> $LOGFILE
else
    rem-sites;
     echo $SDATE: Sites removed >> $LOGFILE
fi


My sites file looks like this

Code:
127.0.0.1 facebook.com # delete
127.0.0.1 myspace.com  # delete


Correct me if I am wrong, this is only from guessing in my head, but when creating a new crontab entry it should look like this:

Code:
* */7,17 * * * /home/user/scripts/script.sh 2& > 1


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP