Linux as an alarm clock

Richard Barrell - January 8th, 2010

I’ve yet to find a cheap and reliable alarm clock loud enough to rouse me from bed in the morning. Fortunately, I already have a laptop with workable (if small and tinny) speakers. All that’s needed to turn it into a loud alarm clock is a little software. Here’s an easy way to do it in Linux:

I want to be jolted from sleep at 8:00AM every day except Saturdays and Sundays. Good old fashioned Unix cron is a perfect fit for this requirement. You can edit your crontab quite easily by running:

env VISUAL=gedit crontab -e


Add the following lines to make cron run the program ~/bin/wakeup every weekday at 8:00AM:

# minute hour day month dayofweek    command
  0      8    *   *     mon-fri      /bin/sh ~/bin/wakeup

Cron isn’t always enabled by default in some distributions. See your distribution’s documentation for how to turn cron on. For Ubuntu users, see Ubuntu’s Cron Howto.

You can test that cron works by adding

 * * * * * mkdir -p ~/crontest

to your crontab, then waiting for sixty seconds and checking that a directory called ~/crontest has been created. If it has, then remove the test line from your crontab and rejoice that cron works. icon smile Linux as an alarm clock

I put my wakeup script in ~/bin. You can create the file using the commands:

mkdir -p ~/bin      # make sure the directory ~/bin exists
gedit ~/bin/wakeup  # edit the wakeup script

The contents of my wakeup script are pretty simple. The first thing I do is turn my speakers on at full blast using ALSA’s non-interactive mixer client. (In case you’re wondering, there is a friendlier way to set ALSA’s volume from a terminal – just use alsamixer.)

Change the values from 100 to something more reasonable if this is too loud.

# Note that "Speaker" might be called "Front" instead on some machines.
# If you get the error "amixer: Unable to find simple control 'Speaker',0",
# then try using the other name.
amixer sset Speaker 100 unmute
amixer sset Master 100 unmute

Next, I use mpd to start blasting music. I find mpd exceedingly handy because it runs unobtrusively in the background, and has a variety of frontends – there are about eight graphical clients for mpd at my last count, one that runs nicely in Terminal using ncurses, and one plain, boring, easily-scriptable command-line client called “mpc”.

Setting up mpd and showing it where your music is and everything is a little more involved than I actually want to go into here right now. Fortunately, there is a good guide to setting up mpd in Ubuntu in Ubuntu’s community documentation.

Having mpc throw away the current playlist and replace it with a new one is quite easy.

mpc clear
mpc load awaken
mpc random on
mpc repeat on
mpc play

This loads and starts the playlist called “awaken” – which on my machine I’ve filled (in advance) with nice, loud rock music, the odd bit of metal, and at least one song by Aqua.

I’ve deliberately left shutting it up afterwards out of my wake-up script; I’m much more likely to make it out of bed if I need to actually walk across the room, manually fire up Terminal, and type

mpc stop

in order to kill the Aqua.

If you do really want to make the wake-up script shut up after ten minutes, then add to the bottom of the wakeup script:

sleep 600  # leave mpd playing for ten minutes
mpc stop   # and then go quiet

Before you kick off your shoes and hit the hay, it’d be a wise idea to make sure that the wakeup script will actually work when cron tries to run it. You can run it manually with the command:

/bin/sh ~/bin/wakeup

If that works then you’re done. Sweet dreams! icon smile Linux as an alarm clock

Tags: ,

One Response to “Linux as an alarm clock”

  1. This is my version that uses amixer and audacious controlled through audtool: http://www.docplanet.org/linux/turn-your-linux-machine-into-wakeup-alarm/

Leave a Reply