Screw your contrast

January 23, 2004

Cron-MP3 alarm clock

Instead of shelling out money on an alarm clock, why not use your Linux system to make sure you get up for work on time.

In order to create our alarm clock we’re going to use the following tools on your linux system…

If you’ll type the command crontab -l you’ll see the contents of your personal crontab file. It may be empty, depending on distribution. If you’re not able to modify your crontab file, as root execute the following command echo <your-username> >>/etc/cron.allow

Note:If the /etc/cron.allow file exists, then you must be listed therein in order to be allowed to use this command. If the /etc/cron.allow file does not exist but the /etc/cron.deny file does exist, then you must not be listed in the /etc/cron.deny file in order to use this command. If neither of these files exists, then depending on site-dependent configuration parameters, only the super user will be allowed to use this command, or all users will be able to use this command. For standard Debian systems, all users may use this command.
If it’s not empty, you’ll see something similar to the following…

20 6 * * 1-5 /home/grim/alarm
This is the guts to telling cron what you want it to do. So what does all the above gibberish actually mean? A crontab entry consist of seven fields separated by spaces. Each field is detailed below…

So, this crontab entry executes the command alarm at 0620 (6:20a.m.) every day of every month that falls on Monday through Friday. So, now you’ll need a script (which I call alarm) to actually play music at an ungodly level to make sure you wake up.

All of the music on my system is in ogg format, they’re smaller in filesize and they sound better. However, this script will work with either MP3 or ogg.

Using bash from the commandline enter the following commands

find /home/grim -name \"*.mp3\" -print >>.playlist;find /home/grim -name \"*.ogg\" -print >>.playlist <return>
Substitute grim with your username. This will find every MP3 and ogg in your home directory and create a playlist called .playlist. You can call it whatever you want, the leading dot will hide it from the normal directory listing. You may want to open this file in your text editor of choice and remove those entries of music or random sound files you may have in your home directory that you don’t want to wake up to.

Once you have a playlist created, open an editor and copy and paste the following code and save the file as alarm

#!/bin/sh
mplayer -really-quiet -shuffle -playlist /home/grim/.playlist
Substitute grim with your username. Make this file executable by typing the following code on the commandline chmod 700 alarm

This script tells the program mplayer to randomly (-shuffle) play all the songs listed in your playlist ( -playlist /home/grim/.playlist) and suppress almost all of the program messages/warnings (-really-quiet).

Once you have your script, we’ll generate our crontab entry. From the commandline enter the command crontab -e This will place you in edit mode, usually in the vi or vim editor. Press the i key on your keyboard to enter insert mode and type the following line substituting grim with your username…

20 6 * * 1-5 /home/grim/alarm
Press the enter (or return) key and then the escape key on your keyboard. Now type :wq and press the enter (or return) key to save your crontab. If you were successful, the shell should report…

crontab: installing new crontab
If not you should get
errors in crontab file, can't install.Do you want to retry the same edit?
Type y and check over your entry again to make sure you didn’t type anything wrong.

This will cause cron to start playing music at 0620 (6:20a.m.) every Monday through Friday. Make sure the volume on your stereo is cranked up before you go to sleep and you should be awakened to a random selection of your music collection in the morning.

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