Jan 18
How-to: Use Your Ubuntu Computer as a Music Alarm Clock
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…
- 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.
- 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
- 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.
- 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
- 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.



January 24th, 2008 at 2:05 pm
I’m sure it’s possible, but how hard would it be to set different alarm times for different days? As an example, Mondays and Tuesdays I need to wake up earlier than the rest of the week, so how would I set those days individually? I guess I could do the whole thing twice, once for the first 2 days and then again for the rest of the week, but that sounds messy.
Also, is there a way to stop the alarms from occurring? Holidays, sick days, vacation, etc. I don’t want to be waking up if I don’t have to!
January 24th, 2008 at 5:55 pm
Responding to your first question, the easiest way is to simply have to entries. For example, I wake up a little later on Fridays, so I have the following 2 entries:
30 7 * * 1-4 {command}
45 8 * * 5 {command}
To turn off your alarms for holidays or when you leave on vacation, you can simply “comment out” your alarm. Comment lines in your crontab is any line that starts with ‘#’. So, simply change each line you want to disable to something like:
# 30 7 * * 1-5 {command}
Enabling it again later is only a matter of removing that hash mark.
Also, I haven’t had a chance to try it yet, but it looks like there is a package called “gnome-schedule” which is a GUI front-end to cron–cool!
March 10th, 2009 at 7:03 am
this tutorial is really great, but i have only one question….does all this work if the computer is suspended to ram?
March 19th, 2009 at 9:18 am
This tutorial is okay, but there are a few little tidbits that you might want to include in this article (or in a linked article). No, I’m not trying to hijack your tutorial… just three suggestions that would have helped tremendously.
First, I liked your idea about using a crontab file in your /home/user (~/) directory. I kept running into a problem that (in Ubuntu) ‘crontab -e’ kept editing (more on this in my second point) a crontab file in the /tmp directory. I had to read around, and eventually I found the little tidbit (in the crontab manpage) about ‘crontab [-u user] file’, which allows you to set where a user’s official crontab is created/used. Ubuntu still copies the crontab to /tmp, but that is a rights thing… unix/linux doesn’t like for you to do direct editing of the active crontab file, so crontab, the program, (in Ubuntu) is written to copy the crontab file to /tmp, edit it there, and then copy your changes into place after it frees up the target crontab file. Tricky, but not something that a normal user should be concerned about… except to ignore the file name one in the editor of choice.
Second, You mentioned “the default text editor” and suggested that it was set to vim. Not so. I’ve never changed it in Gutsy, and it was set to nano. For ease of use, I added –
# set the default editor to gedit
export EDITOR=gedit
into my ~/.bashrc file. I re-ran my ~/.bashrc (was it ‘. .bashrc’ or ‘./.bashrc’ to re-run?)
Third, I saw another site’s comments about using a GUI alternative for setting/creating the cron (which was incomplete).
In Ubuntu,
3a) there is the command-line crontab that everyone should know and love.
3b) There is a gcrontab tool, which I found to be very easy for the non-programmer to use. For the casual administrator, it should be the perfect solution, and is completely compliant with the standard crontab file. Being a programmer, I didn’t really like having so many clicks to enter a crontab line, but I like that it is organized
3c) Another site’s comments raved about KCron. WI like KCron’s design/function much better than gcrontab, and KCron performs a “dual” function under KDE. It is the scheduler, and it can set environment variables, appropriate in the KDE scheme. KCron didn’t ask what Crontab file to load or show anything in my current (by then) ~/crontab file. I assume that KCron is not crontab compliant, since it didn’t automagically import my current ~/crontab file. So, unless you want to use KCron exclusivelyon a KDE instead of the Unix/Linux standard crontab file, I’d stay away from KCron.
Mine are purely editorial/informative comments. Your tutorial is very good, minus the explanation of how to set the user’s crontab file to ~/crontab (a most important tidbit), and the suggestion that vim is the default editor.
Peace,
Barney
September 12th, 2010 at 1:11 am
Thank you. Simple & customizable tip. Looking forward for more.