There are many music players that offer the functionality to wakeup to a song or playlist– both on Linux or Windows. But with each option I’ve tried, I’ve never been really happy with the results. For such a simple task, it always seemed overly-complicated. Also, the main downside I found in using a media player plugin, is that you’ll need to have the player running for it to actually work in the morning. Below I describe how to create your own music alarm clock, using only command-line utilities found on most Linux distributions. It uses quite a few different tools, and the tutorial will hit on quite a few different concepts. So, without further adieu…

  1. The first thing you will need is to create a playlist. I used Rhythmbox, since that’s where I store all of my music anyway. Create a playlist with songs you’d like to wake up to. When you’ve got enough, save it in .m3u format, somewhere where you’ll find it later. I put mine in my home directory.
  2. Next, we’ll need to make sure we have all the tools for the job. We’ll be using cron to schedule our tasks, amixer to set our volume, and mplayer to finally play our music. To make sure you have each of these installed, issue the following command:

    sudo aptitude install cron alsa-utils mplayer

  3. Next, we need to actually add the scheduled task. First I’m simply going to give you the commands, and I’ll explain what’s going on afterwards. In a terminal, enter the command:

    crontab -e

    Note: this will open your default text editor, which if you haven’t set it, will probably default to vim.

  4. Go to the end of the page by pressing Shift+G. Then start a new line pressing “o”. Once you’re there, type in or paste the following line:

    30 7 * * 1-5 /usr/bin/amixer set PCM 35\% && /usr/bin/X11/xterm -display :0 -bg black -fg white -e /usr/bin/mplayer -shuffle -playlist ~/.alarm-playlist

  5. Press “ESC” to stop typing. Then enter the command “:wq” (no quotes) to save and quit. If everything went well, you should see the line:

    crontab: installing new crontab

Cool, you’re done! Now, let me explain what all that was, so you can go back and customize it on your own. Remember, any time you want to learn more about a command, you can use the “man” command. For example,

man crontab

to learn about crontab.

So first of all, we used “aptitude” to install a few packages from the Ubuntu repositories. This is probably familiar to you, or you may be using “apt-get”. They are basically the same, but “aptitude” has a few advantages– you should switch to using it if you haven’t already.

Then, we used “crontab -e”. Cron is the name of the task scheduler in Linux, and this command opens up our own personal “scheduled task list”. You can always use “crontab -e” to edit your tasks, or “crontab -l” just to view them.

Now, on to that crazy line I had you type in:

30 7 * * 1-5 /usr/bin/amixer set PCM 35\% && /usr/bin/X11/xterm -display :0 -bg black -fg white -e /usr/bin/mplayer -shuffle -playlist ~/.alarm-playlist

Each entry in your personal crontab has the following format:

minute hour day-of-month month day-of-week command

So, in our case, our “minute” is 30, “hour” is 7, day-of-month is * (any), “month” is * (any), “day-of-week” is 1-5, and “command” is… the rest of that. This basically means that we’ve scheduled our command to execute at 7:30 am on Monday through Friday. Changing these options should be self explanatory. Now, let’s pick apart our “command” one part at a time.

/usr/bin/amixer set PCM 35\%

First thing to note, is that it’s a good idea to use full paths for any command you execute from cron. To find out the full path to a command, use

which {command}

In this case, we’re using amixer, which is a utility for changing the volume on your computer. I set mine to 35% to wake up to, but you can use anything. Also note here that we can’t use simply “35%”, because cron uses ‘%’ as a special character. Therefore, we preceed it with ‘\’.

Next thing to notice is “&&”. This essentially strings two commands together– it won’t start the next command until our first one has finished. So, onto our next command:

/usr/bin/X11/xterm -display :0 -bg black -fg white -e …

This is actually another compound command. xterm is another terminal that we are going to launch our music alarm in, so we can easily shut if off in the morning. We set all sorts of parameters to make the terminal look nice, but the important one is following the “-e”: that’s the command we will run in the new terminal:

/usr/bin/mplayer -shuffle -playlist ~/.alarm-playlist

Ahhh, finally, this is where we finally play our music. mplayer is a command-line music player with a very basic interface, and easy controls. You can run this line in a normal terminal now to make sure it works. We use the parameters “shuffle” to randomize our playlist, and then “-playlist …” to tell it what to play. Make sure you change “~/.alarm-playlist” to your own location.

And we’re done! At this point you should have a fully-functioning music alarm clock. Now go back and tweak it out with preferences that work for you.

Bonus: Move the alarm clock command to a shell script, and keep increasing your volume every minute or so.

Advertisements