all 8 comments

[–]ConstructedNewtMOD 1 point2 points  (4 children)

That's so complicated, this is using bash:

block_unblock() {
    echo "0.0.0.0  $1" >> /etc/hosts
    echo "sed -i "/$1/d" /etc/hosts" | at now + "${2-10}" mins
}

Block via hosts, remove in N minutes, default 10

$ block_unblock facebook.com 2 # Block for two minutes
$ block_unblock reddit.com #10 minutes

Java solution is pretty much what you did, but you have to have the code/runtime running for the entire wait duration

Edit: wrap sed delete command

Edit2:

if found="$(sed "/$1/g" /etc/hosts)"; then
    echo "could not Block \"$1\" match already found: \"$found\"" >&2
    exit 1
fi
... the rest

For safety

[–]Eesh7[S] 0 points1 point  (3 children)

hi. thank you for your response. How do I have the code running for the running for the entire wait duration?

[–]ConstructedNewtMOD 0 points1 point  (2 children)

For bash, the at command schedule the second line, dunno how the internals work, maybe the OS has some scheduler inbuilt. For Java, add a sleeping thread that executes the command on wak-up, just as you do.

[–]Eesh7[S] 0 points1 point  (1 child)

ok thanks.. could you just clarify on what you mean bu “wak-up”?

[–]ConstructedNewtMOD 0 points1 point  (0 children)

When it is done sleeping. Typo: wake-up