Skip to content

Instantly share code, notes, and snippets.

@jwm-art-net
Last active February 28, 2026 18:45
Show Gist options
  • Select an option

  • Save jwm-art-net/27262613bccd080316e8a8eb67f16e38 to your computer and use it in GitHub Desktop.

Select an option

Save jwm-art-net/27262613bccd080316e8a8eb67f16e38 to your computer and use it in GitHub Desktop.
#!/bin/bash
# block_website_time
# block websites by time of day
# run as a cron job, initially i ran this once a minute
# but then tried once every ten minutes... which means i can manually
# edit the hosts file and get a short window of access.
# too easy though, so now it's once every 5 minutes.
# this is the 2nd main version. the main difference from the initial
# version is the chunk of entries this script generates is deleted
# and recreated on every run. the initial version used sed to search
# and replace make comment out entries (or not) when unblocked (or not)
# which meant the script could be more easily circumnavigated.
# this can still be circumnavigated, it's not meant as security, it's
# for personal use by people who *want* a mechanism to reduce their
# time on specific websites.
# -----------------------
# options
hostsfile="$HOME/hoststest"
websites=( "facebook.com"
"instagram.com"
"youtube.com" )
blocktimes=("00:00 08:30"
"09:30 12:30"
"13:30 16:30"
"17:30 20:30"
"21:30 23:30" )
# end of options
# ---------------------------------------
# function in_time_range
# option --time "hh:mm" - override use of system time with time specified
# option --result "variable" - specify variable name to store range of test
# (it will store an inbetween range if test is negative) - check return result
# return result is 1 if test positive, otherwise 0.
# specify a range as two hour/minute time values, ie: 09:45 12:15
# multiple ranges may be specified at once
function in_time_range()
{
while [[ $# -gt 0 ]]; do
case "$1" in
--time) checktime="$2" ; shift ;;
--result) result="$2" ; shift ;;
*) break ;;
esac
shift
done
if [[ $# -lt 2 ]]; then
echo "invalid number of parameters for time range."
exit -1
fi
if [[ -z $checktime ]]; then
checktime=$(date +"%H:%M")
fi
currtime=$(date +%s --date="$checktime")
unset secsm1
while [[ $# -gt 0 ]]; do
if [[ $# -eq 1 ]]; then
echo "unpaired number of start/end times."
exit -1
fi
start="$1"
secs0=$(date +%s --date="$1")
shift
end="$1"
secs1=$(date +%s --date="$1")
shift
if [[ $currtime -ge $secs0 && $currtime -le $secs1 ]]; then
if [[ -n $result ]]; then
declare -n ret=$result
ret="$start $end"
fi
return 1
elif [[ -n $secsm1 && -n $result ]]; then
# determine if between previous range and this range
if [[ $currtime -ge $secsm1 && $currtime -le $secs0 ]]; then
declare -n ret=$result
ret="$m1time $start"
return 0
fi
fi
m1time="$end"
secsm1="$secs1"
done
return 0
}
function create_block_list()
{
echo "# $scriptmd5tag"
echo "# Any manual edits should be added before the # MD5 line above."
echo "# Anything added after it could get deleted."
echo
block="# "
in_time_range --result range ${blocktimes[*]}
if [[ $? -eq 1 ]]; then
block=""
fi
block="${block}127.0.0.1"
for i in ${websites[@]}; do
echo "$block $i"
echo "$block www.$i"
done
echo
echo "# block times for websites:"
for i in "${blocktimes[@]}"; do
echo "# $i"
done
echo
echo "# last updated "$(date +"%H:%M")
return 0
}
# --------------------------------------------------
# start of main processing
# sanity checking...
if [[ ${#websites[@]} -eq 0 ]]; then
echo "website array undefined"
exit -1
fi
if [[ ${#blocktimes[@]} -eq 0 ]]; then
echo "block times array undefined"
exit -1
fi
script=$0
scriptmd5tag=$(md5sum --tag $script)
sed '/^# MD5 (.*'$(basename $script)')/,$d' -i $hostsfile
create_block_list >> $hostsfile
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment