Power Nap Via the Command Line in Linux/Mac OS

A month ago, Oscar del Ben posted an interesting tip in his blog to take power naps when you are feeling tired, How to Get a Quick 5-10 Minutes Nap Without Using an Alarm Clock. His idea is simple: pick a spoon on your hand. Once you fall asleep, your grip will relax, the spoon falls and you awaken with the sound. I found it amusing and interesting, as I am one of those types who feel really well after a 10 minutes nap. Thus I wanted to solve some problems of his approach in a Unix way, and I wrote the power nap script.

The catch of Oscar's method? You sleep almost nothing, with this method. You doze, and when you enter real nap mode, you awaken. It can (and does!) refresh your mind, but it does not qualify as a Power nap:
Scientific experiments (see Benefits section below) and anecdotal evidence suggest that an average power nap duration of around 15-30 minutes is most effective. Any more time, and the body enters into its usual sleep cycle. People who regularly take power naps may develop a good idea of what duration works best for them, as well as what tools, environment, position, and associated factors help induce the best results. Others may prefer to take power naps regularly even if their schedules allow a full night's sleep.

Thus the ideal sleep length is around 15 minutes. How do you get yourself to sleep 15 minutes? I find it very hard, because a power nap is ideally taken after lunch, when you may be sleepy, but not that sleepy to fall asleep instantly, less if you are sitting in your office chair. You can estimate how long it takes (for you!) to fall asleep, but from my own experience, this varies greatly. I usually take around 5-15 minutes at night, but can take around 20 minutes when it is daytime. How to sleep for exactly 15 minutes after falling asleep?

The solution is pretty easy: what do you have in your office, always on? Yes, your computer. If you are a Windows user, I am sorry: my solution works in Linux or Windows. You can use Cygwin, or fight your way to implement the same in your own platform. I encourage you to do so!

What is this solution? A power nap bash script, with several command line parameters. Once started, it waits for a second for a key press. If a key is pressed constantly, i.e. you have your finger over a key, nothing happens. As soon as you release the key, a counter starts running, and when it finishes your computer plays a beeping sound or a file of your choice.

The idea is that you get in a comfortable position in your desktop, start the script and keep your finger pressing on a key. You have to be in a position such that when you fall asleep, your finger falls of the keybord. One option is to slouch in your chair with your arm extended pressing the space bar, when you fall asleep, your hand will fall over your desktop. Another option is crossing your arms over your desktop, with your head over them and the keyboard on a side. You just press enter with your hand and when you fall asleep your hand will fall.

When you sleep, the counter keeps ticking, and when it ends, music (or console beeps) awaken you. And the power nap is over and you can get away with your terrific job.

Usage is ./nap.sh naptime "musicplayer" or ./nap.sh test "musicplayer". If musicplayer is empty, it will use the system bell, and test is used to be sure it will do something after the selected time. As of this writing, the music file is encoded in the script (feel free to change it to some file you like!), and you have to use the music player best suited to your system. In my MacBook, I would use open as music player, in my NanoNote or netbook, madplay (command line music player) with the option -a -30 to adjust the volume.

An example use (the way I use it in my NanoNote) is ./nap.sh 15 "madplay -a -20"

You can get the most up-to-date script from my Google Code repository here: nap.sh. In a future version, I will add a safety control to beep after naptime+safetymargin minutes, to solve the problem of pressing a key and then falling asleep over the keyboard.

If you have any suggestion or improvement to the script, or have used it please drop by and leave a comment! Also, share with your friends in Reddit, HackerNews, Digg or StumbleUpon.

Related posts:
9 programming books I have read and somewhat liked...
Linux is a time killer (follow-up – Ditching Ubuntu: Arch Linux in my Acer Aspire One)
My first port to the Ben NanoNote: gnugo
Power to the command line
Two weeks, still loving Fluxbox
Three dee (3-dimensional file system browsers review)
Gcal: the ultra-powerful command line GNU calendar
Acer Aspire One 8.9' + Ubuntu + Fluxbox
Written by Ruben Berenguel