Tutorial - Set up an MP3/OGG Alarm Clock using Linux

Alarm Clock

Tip: you might be interested in my DVD ripping guide ^_^

Why should you spend money for an alarm clock or a software that does an equivalent job, when you can just use you Linux box? Well, most likely you use cellphone... but if you have the computer switched on all day long you can also try and do it yourself. You may already have all the software that you need to make it work, it's done by you, it's highly customisable, and above all it's more fun. This short tutorial will guide you step by step in creating an MP3/ogg alarm clock using your computer, it works with all Linux systems, and can easily be modified for MAC OS X, *BSD, well you got it.

System requirements.

Let us now begin. In order to create our alarm clock we’ll be needing the following packages:

I take that you all know how to install a package, I recommend, depending on your system, to use your distribution's repository. So if you have a debian based distro you will have to type as root something like:

apt-get install cron bash find amarok

Mandriva uses urpmi instead of apt-get, Gentoo has its beautiful emerge and so on. I strongly suggest to compile the MPlayer package yourself, and specifically from SVN. There are several reasons for this choice.

  1. The SVN package contains all the newest patches and functionalities.
  2. MPlayer runs much faster if compiled, the MPlayer developers themselves encourage to compile from source, quoting them:

    "The recommended way to install MPlayer is to compile from source. Look at the unofficial packages section of our projects page if you do not wish to compile from source and/or are looking for packages that may be more tightly integrated with your platform. Just remember that we do not support any unofficial packages except the official ones listed below."

  3. MPlayer is a fast growing project. even the smallest change in the code can mean a precious help, if you had to install a new version every time you'd go crazy, using the CVS version allows you to upgrade the package in a matter of seconds.

Issue the following command to get the latest sources:

svn checkout svn://svn.mplayerhq.hu/mplayer/trunk mplayer

A directory named mplayer will be created in the current directory. You can later update your sources by typing

svn update

from within that directory.

Getting started, using cron

The command crontab -l shows you the content of your personal crontab file. It may be empty, depending on your distribution. In case you are not able to modify your crontab file, just execute as root the following command

echo <your-username> >>/etc/cron.allow

this will allow you username to modify the cron events, vice versa for the /etc/cron.deny file, make sure you do not create a conflict of names in these files (user_A cannot be present in both files).

If the file is not empty something will appear, mine is like this:

#m h dom mon dow command
42 07 * * 1-5 /usr/local/bin/alarm

Let me explain what is the meaning of this line. A crontab entry consist of seven fields separated by spaces. Each field is detailed below.

  • minute – This controls what minute of the hour the command will run on, and is between 0 and 59
  • hour – This controls what hour the command will run on, and is specified in the 24 hour clock, values must be between 0 and 23 (0 is midnight)
  • dom – This is the Day of Month, that you want the command run on, e.g. to run a command on the 19th of each month, the dom would be 19.
  • month – This is the month a specified command will run on, it may be specified numerically (0-12), or as the name of the month (e.g. May)
  • dow – This is the Day of Week that you want a command to be run on, it can also be numeric (0-7) or as the name of the day (e.g. sun).
  • cmd – This is the command that you want run. This field may contain multiple words or spaces.

So, this crontab entry executes the command alarm at 07 42 (7:42a.m.) every day of every month that falls on Monday through Friday. Now what you need is a bash script called alarm that will actually play some music at the desired time to make sure you make up. if you want to modify you contrab entry all you have to do is to type

crontab -e

as normal user, then insert the line a posted before, according to your needs of course.

I tend to have all my music in ogg format. The same piece of music, if it's Vorbis is smaller in filesize and it sounds better than an mp3 or anything else. However, this script will work with either MP3 or ogg.

Open a terminal and type (you can just copy-paste it):

 find ~/ -iname "*.mp3" -o -iname "*.ogg" > .playlist

This will find all the music you have in your home directory. If the music is placed somewhere else simply replace the ~/ with the absolute path of your directory, you can concatenate other commands for aac or mp4 files in a similar way. A playlist called .playlist is created and place in your home directory as a hidden file, so that it does not bother you.

We have configured cron to start the command alarm, but we don't have it yet, we need a script! Open your favourite text editor (I personally use Vim, but you can use whatever you like, Emacs, Gedit, Kate, Kwrite, Scite...) and type:


# Uncomment if you want to use amaroK
#/usr/bin/dcop /usr/bin/amarok player play

#Comment if you do not want to use MPlayer
/usr/bin/X11/xterm -display :0 -bg black -fg white \
-e /usr/local/bin/MPlayer -shuffle -playlist ~/.playlist

# Eterm solution#/usr/bin/Eterm -0 -e /usr/local/bin/MPlayer \
# -shuffle -playlist ~/.playlist

Now in a terminal:

chmod 700 alarm
mv alarm /usr/local/bin

The script has two possibilities listed. The first one is commented and it allows the amaroK player to start using the DCOP engine. The advantage of this method is that you will be able to fully use amaroK's huge potential, and not merely start some music. I will leave apart the description of all the incredible functionalities that amaroK has, because it is not the purpose of this tutorial, I will just redirect you to its website for further details.

The second one uses the omnipotent MPlayer. MPlayer is fast, works every
time and with basically every existing format, it's easy to recompile and it depends from very few packages. The script will open an xterm and then execute MPlayer in shuffle quiet mode, so that you will be able to control it once opened. Just pick one of the two possibilities, it depends from your needs, I personally know many people that the only command that starts with K on their computer is kill, others have 767 programs starting with K. Just a matter of opinion. Obviously there is no such a thing as "the perfect option", it may vary from system to system, either choose the one that suits you better or create one of your own.

You may create custom playlists, depending on your mood. This is an example of how it can be accomplished, but with some little bash knowledge you will understand that the number of possibilities is infinite. Imagine that you would like to listen to your Radiohead songs.

find ~/ -iname "*.mp3" -o -iname "*.ogg" | grep Radiohead > .playlist_radiohead

You may even automate this process and easily create custom playlists. Take this:

PGM="Alarm version 0.01"

echo ""$TITLE" "$PGM", playing "$BAND"
find ~/ -iname "*.mp3" -o -iname "*.ogg" | grep "$BAND" > ~/.playlist
/usr/bin/Eterm -O -e /usr/local/bin/MPlayer -shuffle -playlist ~/.playlist

Save the script as calarm and follow the standard procedure described above. When lanching the script you will provide one argument, that is the name of the band. When launching the program, something like thsi will appear:

¤ calarm Radiohead
calarm - Custom Alarm Alarm version 0.01, playing Radiohead

If no argument is provided, the script will run all your collection. This process is supposed to be much slower, because every time it searches for new music, but in reality... this is my benchmark.

¤ time calarm
calarm - Custom Alarm Alarm version 0.01, playing

real 0m0.133s
user 0m0.044s
sys 0m0.056s

¤ wc ~/.playlist 
 840 6878 86735 /home/phate/.playlist

This means that it took 0.1 seconds to scan and create a collection of 840 songs. Even if you have 20 thousand songs, it would take no more than three seconds.

If you’d like to learn more about the inner workings of cron read the Newbie Intro to cron and the Manpage of cron.

There is no meaning in automating the process, so I did not create a script that does all the job, as I did for the DVD ripping Guide, since most of it cannot be done independently from each person's system. Furthermore, it's more fun this way, and you get to understand what actually happens.

For further information, suggestions, bug fix, send me an email.

This tutorial was inspired by Philip McClure's article.