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…
- cron
- bash
- find
- mplayer
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 theIf it’s not empty, you’ll see something similar to the following…/etc/cron.allowfile exists, then you must be listed therein in order to be allowed to use this command. If the/etc/cron.allowfile does not exist but the/etc/cron.denyfile does exist, then you must not be listed in the/etc/cron.denyfile 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.
20 6 * * 1-5 /home/grim/alarmThis 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…
- 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.
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/.playlistSubstitute
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/alarmPress 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 crontabIf 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.
Posted by Philip McClure in Linux on January 23, 2004