One of my practical life hacks is having my computer turn off automatically at night, periodically, between 11pm and 7am. I'm guessing this gets to me to bed between 30 and 90 minutes earlier each night. 

I can interrupt this process if I want to keep working (or screwing around on facebook), but it requires a manual override. If I'm unreflectively screwing around on facebook I probably will not remember to hit the manual override before the computer turns off.

Someone recently asked how to do it. It requires some coding, and knowledge of bash and cron jobs. It works specifically on macs. (Please be careful about mucking around with cron jobs and auto shut downs. If is possible to accidentally modify this into a script that shuts down your computer every minute, which is annoying to undo. Be cautious using Magical Scrolls You Get From Wizards You Don't Understand).

Here's a writeup.

I've created a little bash file called sleepScript.sh, which looks like this:

if [ -f /Users/[USERNAME]/documents/sleepScript/do_it.txt ]; then
    /sbin/shutdown -h now
    touch /Users/[USERNAME]/documents/sleepScript/sleep_times.txt
    echo "$(date)" >> /Users/[USERNAME]/documents/sleepScript/sleep_times.txt
else
    touch /Users/[USERNAME]/documents/sleepScript/do_it.txt
    touch /Users/[USERNAME]/documents/sleepScript/sleep_times.txt
    echo "$(date)" >> /Users/[USERNAME]/documents/sleepScript/sleep_times.txt
fi

This hacky little script checks if a particular file exists in a particular location, called "do_it.txt". If it exists, it shuts down my computer. Otherwise, it creates the file. I can "manually override" it by deleting the file.

Either way, it also manually logs the time, which I sometimes use to check how late I was up a given night.

To trigger the script to run at night, I added it as a crontab, which requires first editing the crontab file. Because forcing a shutdown requires a sudo override, you also need to do the sudo override when editing the crontab:

sudo crontab -e
0 23 * * * sudo bash /Users/[USERNAME]/documents/sleepScript/sleepScript.sh
30 23 * * * sudo bash /Users/[USERNAME]/documents/sleepScript/sleepScript.sh
0 0 * * * sudo bash /Users/[USERNAME]/documents/sleepScript/sleepScript.sh
30 0 * * * sudo bash /Users/[USERNAME]/documents/sleepScript/sleepScript.sh
0 1 * * * sudo bash /Users/[USERNAME]/documents/sleepScript/sleepScript.sh
30 1 * * * sudo bash /Users/[USERNAME]/documents/sleepScript/sleepScript.sh
0 2 * * * sudo bash /Users/[USERNAME]/documents/sleepScript/sleepScript.sh
30 2 * * * sudo bash /Users/[USERNAME]/documents/sleepScript/sleepScript.sh
0 3 * * * sudo bash /Users/[USERNAME]/documents/sleepScript/sleepScript.sh
30 3 * * * sudo bash /Users/[USERNAME]/documents/sleepScript/sleepScript.sh
0 4 * * * sudo bash /Users/[USERNAME]/documents/sleepScript/sleepScript.sh
30 4 * * * sudo bash /Users/[USERNAME]/documents/sleepScript/sleepScript.sh
0 5 * * * sudo bash /Users/[USERNAME]/documents/sleepScript/sleepScript.sh
30 5 * * * sudo bash /Users/[USERNAME]/documents/sleepScript/sleepScript.sh
0 6 * * * sudo bash /Users/[USERNAME]/documents/sleepScript/sleepScript.sh
30 6 * * * sudo bash /Users/[USERNAME]/documents/sleepScript/sleepScript.sh

There wasn't an elegant way to set the specific times I wanted my computer to shut down. I have it shut down once per half hour in between 2300 (aka 11pm) and 6:30am. (I've also experimented with having it shut down more often later at night, like every 15 minutes)

Hopefully this is helpful to someone. 

New Comment
13 comments, sorted by Click to highlight new comments since: Today at 9:20 PM

Instructions for Windows 10 (search for "Schedule for Automatically Shutting Down").

To cancel a shutdown, type "shutdown -a" in the command prompt or the "run" box.

My biggest worry with something like this would be data loss; some programs and web sites don't have good autosave, so unexpectedly shutting down would risk losing work. Delivering a notification 20 seconds before shutdown would help by giving a chance to save; the challenge, then, is to make it so that overriding it is easy to do before the notification arrives, but hard to do afterwards.

FocusMe can do this.  Both give the reminder, and make it easy to stop before the time starts, and hard to stop afterwards.

FWIW I have never had a problem with this in 3 years

(update: I had some data loss stuff during the pandemic because I started using some older/less advanced programs to do graphics and level-design for Gather Town. But I haven't had the issue with most text editors or photoshop or whatever because of autosaving)

There wasn’t an elegant way to set the specific times I wanted my computer to shut down.

You could change the script to check the time and configure cron to run it every 30 minutes, all day.

H=$(date +%H)
if [ $H -gt 8 ] || [ $H -lt 22 ]; then
# Don't try to shut down
exit
fi
# Script to try to shut down goes here

Many routers have controls for when devices can access the internet or be blocked, either blanket or for specific devices. Generally, this requires logging in to the router UI to override it, so is less preferable than host-based options. But it works for everything, including streaming devices and consoles.

For Macs you can also use Screen Time to disable various things according to a schedule like 11pm to 7am. SelfControl is also an option but only deals with internet connectivity, not offline stuff.

For Windows (it also runs on Mac but I've never used it), I highly recommend https://focusme.com/. 

It can block both apps and websites, and has all the tools needed to make a sleep blocking plan work.

I have an "enforce sleep" plan, that prevents me opening every website and app, except my calendar after 9:45 (I check my calendar before bed so I know what to time to get up the next day). 

I have it set so that by entering a random string of characters, I can pause the computer for up to 30 minutes, 3 times.  This successfully gets the "I can still do the computer if I want to" thing without me having to think about it.

I also have it set up to automatically re-enable itself every morning.  This is pretty essential because sometimes I'll have late night video calls with people from other timezones,and I can't have my computer randomly shutting down.  So I need to turn off the plan before the meeting. But I also don't want to have to remember to re-enable something that night or the next day.   (This would probably cause the bash script to be a non-starter for me).

Yeah. The above setup is in addition to Freedom.to, which is my primary "control online habits" tool. Typically at night I want to be able to do particular kinds of screw-around-on-the-internet, and the problem is that I just end up doing it too long, unreflectively.

A key piece of the setup is that I'm allowed to turn the computer back on, as many times as I want, but each time it forces a deliberate choice, and gives me a moment to notice "you know what? I'm tired, and youtube isn't actually that good."

Someone recently asked how to do it.

This link doesn't work.

Hopefully fixed.