I bought a small alarm clock when I started college almost two years ago. I currently have no idea where that alarm clock is. Over the past two years I’ve come to realize that I don’t like to have a bunch of different gadgets doing different things, especially if I can do it on a general purpose computer. I’ev been using a software alarm clock for over a year now. Mostly I use the excellent Alarm Clock 2 for OS X, but over summer I needed to have an alarm clock on my Linux laptop. There are a number of Linux alarm clock programs out there including KAlarm and the imaginatively named Alarm Clock for GNOME. However, being the minimalist that I am, I was interested in seeing if I could get what I wanted without a completely separate program. Also since I run a minimal XMonad desktop, I didn’t want to install GNOME or KDE centric apps. What I found out was that it’s really easy to roll your own alarm clock using tools that you already have.
Gathering the tools
I used three separate programs and a small shell script to create my personal alarm clock. To actually produce the sound for the alarm I used the Music Player Daemon. As the name suggests it’s a daemon that runs in the background and can be used to play music files. Since it’s a daemon you need a separate front end to communicate with it. It’s a really useful application and there are a number of great front ends to get going. The second program I needed was a simple front end to get the daemon to play something. Normally I use the excellent ncmpc client to listen to music, but for the alarm clock I decided to install the simpler mpc client which offers some simple commands for manipulating a playlist and playing it. Finally I need a way to actually get it to play at certain times. For this I used another classic UNIX daemon called cron.
Putting it all together
Now since I’m probably going to be sleeping when I want the alarm to go off, I needed to have the mpc client automatically get some music from my collection and play it. Getting it to play music involved a simple three line shell script:
mpc clear mpc search title Universe | mpc add mpc play
The first line clears any playlist that I may have played last. The second line searches the MPD database for songs with the word ‘Universe’ in the title (I wanted the really loud Center of the Universe by Kamelot which is almost impossible to sleep through). The final line simply tells mpc to go ahead and get started. I saved this shell script in a file called
alarm and made it executable by doing a
chmod u+x alarm.
With the script ready to play music, I had to make sure that it was executed at the right times. Cron is a daemon which is made exactly for tasks like this. It uses a really simple rule format to specify timing and repetitions and commands to execute at those times. A typical cron rule has the following format:
Minute Hour Day_of_Month Month Day_of_the_Week Command
Rules for cron are saved in a file which can be edited using the
crontab -e command. But I found it easier to just put the rules in a separate file and then give that file to the crontab command to use. Shown below is what my current crontab looks like. I have the alarm shell script being executed 4 times a day, everyday (that’s because I’m trying to adjust to a dymaxion sleep pattern).
45 00 * * * ~/src/scripts/alarm 45 06 * * * ~/src/scripts/alarm 45 12 * * * ~/src/scripts/alarm 45 18 * * * ~/src/scripts/alarm
I’ll be the first to admit that it isn’t the perfect alarm clock. There’s no snooze or gradually increasing volume and if I want to stop I either have to type in the
mpc stop command or else stop MPD via another front end. If I wanted a more sophisticated alarm clock, I’d probably write a more powerful wrapper around MPD or some other player in Python to get the extra features I wanted. But this little tool is just enough to be sufficient for my needs. Also it was a good chance for me to learn about UNIX tools like cron.
Cron can be used for a lot of things besides an alarm clock, anything that needs to run on a schedule could probably be turned into a combination of a shell script and a cron job. At the same time, you don’t have to use MPD, there’s a great tutorial that tells you how to use other media players like Amarok or MPlayer. The main thing I’d like to express by writing about little experiments like this is that building your own tools can be both useful and fun and you really should invest some time in learning the tools available to you. After all, a computer really is just a dumb machine and it should work the way you want it to, not the other way around.