Skip to content

Instantly share code, notes, and snippets.

@nolenroyalty
Last active January 2, 2025 21:09
Show Gist options
  • Save nolenroyalty/5c27c7bea201806bf35a99d79debc280 to your computer and use it in GitHub Desktop.
Save nolenroyalty/5c27c7bea201806bf35a99d79debc280 to your computer and use it in GitHub Desktop.
A script I run via cron that prompts me to sleep at night
#!/usr/bin/env bash -x
# TO USE:
# 1. MAKE A CONFIG FILE AT ~/.go-to-bed
# it should look like this (don't include hashes in file, this will nag you starting at 12:16 AM until 6:00 AM)
# START_TIME:0.16
# END_TIME:6.0
# HUSHED_UNTIL:2023-11-23T1:40:00
#
# 2. SAVE THIS SCRIPT SOMEWHERE, maybe ~/bin/go-to-bed
# 3. ADD TO YOUR CRONTAB, mine looks like this:
# % crontab -l
# * * * * * /Users/nroyalty/bin/go-to-bed.sh -from-cron 2>/dev/null 1>/dev/null
#
# 4. TO SNOOZE, EDIT HUSHED_UNTIL AND IT WON'T NAG YOU TILL AFTER THAT TIME
# 5. TO WIN, NEVER EDIT THE FILE TO SCROLL TWITTER
if [ "$1" = "-from-cron" ]
then
if ps aux | grep osascript | grep -q 'Time to Sleep'
then
echo There is probably a pending window, exiting
exit 0
fi
fi
alert_title="Time to Sleep"
alert_message=$(
cat <<'EOF'
Most of the time when you are using your computer at this hour you aren't doing anything worthwhile.
If you're programming or doing something else cool, edit ~/.go-to-bed to hush this message until tomorrow.
Otherwise you should consider sleeping or doing something more fun! Read a book or a new yorker article. Play a game. Play your piano.
EOF
)
start_time=$(cat ~/.go-to-bed | grep START_TIME | cut -d: -f2)
end_time=$(cat ~/.go-to-bed | grep END_TIME | cut -d: -f2)
time=$(date +%H.%M)
convert_to_minutes() {
hours=$(echo $1 | cut -d. -f1)
minutes=$(echo $1 | cut -d. -f2)
echo $((10#$hours * 60 + 10#$minutes))
}
# Convert HH:MM to minutes
start_minutes=$(convert_to_minutes $start_time)
end_minutes=$(convert_to_minutes $end_time)
current_minutes=$(convert_to_minutes $time)
if (( $current_minutes >= $start_minutes && $current_minutes < $end_minutes ))
then
echo in window, maybe notifying
hush_time=$(cat ~/.go-to-bed | grep HUSHED_UNTIL | cut -d: -f2-)
zone=$(date +%z)
sec_hush=$(date -j -f '%Y-%m-%dT%H:%M:%S%z' "${hush_time}${zone}" +"%s")
sec_now=$(date +%s)
if (( $sec_hush > $sec_now ))
then
echo hushed, doing nothing
else
echo notifiying
afplay /System/Library/Sounds/Blow.aiff
say -v Daniel time to sleep
osascript -e "display alert \"${alert_title}\" message \"${alert_message}\""
fi
fi
@nolenroyalty
Copy link
Author

@wongjustin99 thanks!! that seems better although I'm so used to the old behavior idk if I can switch!!

@SilverEzhik
Copy link

Got inspired by this one and made a Hammerspoon version: https://gist.github.com/SilverEzhik/f4626851965fa7a1be0b27bfb49280d1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment