Wake and sleep with cron and rtc

Table of Contents

I've been tuning my schedule using cron and some scripts to wake and sleep the computer, and, accordingly, myself.

1 Basic idea

Fairly recently decided that I want to wake up at 7AM and be ready to work at 8AM. I also want to be able to turn off the computer at 4PM with a clear conscience. That doesn't necessarily mean that I will stop work at that hour, for example, maybe I'll read a book, or write some notes by hand. But the typical idea would be to go out into the outside world. I came to the conclusion that this would be worth trying when I realised that if I also follow this schedule on Saturday and Sunday, I'll have 16 hours a week clearly set aside for my "hobbies" or "side jobs", when I won't be tired or feel spread too thin. Contrast this with a lifestyle in which I try, ineffectively, to cram everything in every day and at all hours, which results in me not getting things done as effectively as I think I could. But old habits die hard, and I'm not the most disciplined person in the world.

2 Implementation

Cron makes some parts of this easy. In my user's crontab (edit via crontab -e):

35 15 * * * /usr/local/bin/ratpoison -c "echo Computer shutting down in 10 minutes."
25 15 * * * /usr/local/bin/ratpoison -c "echo Computer shutting down in 20 minutes."
15 15 * * * /usr/local/bin/ratpoison -c "echo Computer shutting down in 30 minutes."
05 15 * * * /usr/local/bin/ratpoison -c "echo Computer shutting down in 40 minutes."
00  7 * * * mpg321 /home/joe/Downloads/Dialogue.mp3

In root's crontab:

45 15 * * * /home/joe/bin/sleep.sh

where sleep.sh is just the short instruction


/usr/sbin/rtcwake -m disk -u -t $(date +%s -d 'tomorrow 06:55')

2.1 Note

After trying things with this set-up for a while, I noticed a pattern. I was frequently making the (all-too) human error of turning the computer on after I planned to have it off. Well, naturally, I wanted to keep working, or carry on with a phone conversation, or whatever. The solution is simply to repeat the warnings and sleep commands at 7PM and 9PM. That ought to reinforce the basic message that's being sent here. Technical details omitted, I'll report back on how this is working in a bit.

3 Other support tools

In order to enhance my ability to focus during the limited time when the computer is turned on, I've installed LeechBlock, "a simple productivity tool designed to block those time-wasting sites that can suck the life out of your working day." On my blacklist: The Guardian (blocked 0900-1900), YouTube (blocked 0800-1900).

4 Possible extensions

  • … setting the "default" OS in rEFInd to be Ubuntu, so that if I temporarily boot into Mac OS, the morning alarm will still work.
    • partially solved: changing the default_selection in refind preferences on the Linux side, in /boot/efi/EFI/refind/refind.conf to "3" means that the 3rd OS in the list will become the default. However, I think booting any OS still screws up rtc.
  • … a way to defer the automatic shutdown, for example if I'm in a lecture taking notes, or in the middle of a phone conversation. Basically, a modal dialog box that would allow me to buy myself a certain number of additional minutes or whatever.

Author: Joseph Corneli

Created: 2015-07-26 Sun 23:11