Automatically Turning Off Computer at Night

by Raemon1 min read15th Jul 202012 comments

17

ProductivityPractical
Frontpage

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. 

17