A Crontab Alarm Clock
At first glance, the idea of setting up a alarm clock in Linux using cron probably seems very simple to most long term Linux users, and for the most part it is an easy and simple project. Unfortunately, there are a few hangups to creating the system, the main one being that many distributions of Linux require you to launch a terminal for the program to play in (if it is in command line, as in this guide it will be) and to specifiy in which display it appears (this may also have to do with which version of cron you use, but I know for sure that Ubuntu required you to do this, as does Gentoo with Vixie-cron).
The first part of the process is to figure out what commands you need to run for your alarm. I’m something of a heavy sleeper, so I require my alarm to be louder than what I normally have my volume at - this means that I have to control the volume through the command line. Fortunately, this is easily done with the amixer command. I call amixer twice for my alarm, the first time to set the volume at one hundred percent with the command amixer set Master 100%
which runs before the music begins to play, and the second one to set the volume back at a more normal volume which is amixer set Master 30%
and is run after the music plays.
The actual alarm is mplayer, which is run with /usr/bin/gnome-terminal -e "/usr/bin/mplayer /path/to/song(s)"
. Take note of the fact that the full path is used to call the programs. This is because cron does not always correctly use the PATH variable, and so may or may not call the program correctly without the full path. Additionally, cron requires that you open a terminal when you call a command line program, so I run gnome-terminal (the default terminal emulator in Gnome, x-term also uses the same syntax to open programs when you open the terminal) with the -e flag and then enter the mplayer command within quotations. For readers whose programs are in different directories, planning to run other media players for their alarms, or want to run a program that is altogether different, use the command whereis program_name
to find the full path.
An optional component in the music player is to place all the commands into a script, which makes the cronjob much easier to setup. Without a script, three jobs must be created, and any changes to the musical selection or volume must be edited from within crontab. Using a script allows you to only create one cronjob, change the songs or modify the volume controls easily (for example, creating an alarm which gradually increases in volume with each song) and it also means that changing the time the job will run is a one step operation, so if you ever have to get up earlier than normal you only have to modify the single line in the crontab. A sample script (alarm.sh) is attached, the only modifications that should be required to use it are changes in the paths, and making the script executable.
Finally comes the all important step, adding the job to cron (if you choose to run each command individually, simply adjust the time on the jobs so they run in the right order, this guide will use the script for simplicity). First a basic rundown on the parts of a cronjob. There are six columns (some implementations have an extra column for the user the job runs as) each one separated by a space. The first column is the minute that you want the job to run, in two digits (00-59), the next column is the hour (00-24) (an easy mistake make is putting 03 when you mean 3 pm, make sure to use 24 hour time). The third column is for the day of the month, the forth the month (1-12) and the fifth is the day of the week, with each day being represented with one of the numbers 0-7, 0 and 7 being Sunday (from my understanding). This final column would allow you to set two different alarms, one for the weekdays and one for weekends if you chose (or other similar combinations). In order to make a command run each on of the increments (once every minute, hour, etc) place a single asterisk (*) in the column. For more information about the run time for cron, please look up a guide specific to your distribution or cron implementation.
The final column is the command itself, which in this case will be the location of the script prefixed with env DISPLAY=:0.0
. For example, on my system (where the script is located in a directory within bin called cron) my command is env DISPLAY=:0.0 /home/eugene/bin/cron/alarm
.
Now that you have worked out exactly what you are going to put into cron, it is time to implement this solution. Run the command crontab -e
as whatever user you desire the alarm to run under. Enter the information for the time in the order previously described, and then the command, making sure to keep a space between each column. Once done, it should look something like this 00 04 * * * env DISPLAY=:0.0 /home/eugene/bin/cron/alarm.sh
. By the way, if you plan to test the job right away by setting it to run right after you set it, make sure to give it at least five minutes, so that cron has time to pick it up.
#!/bin/bash
mixer set Master 100%
/usr/bin/gnome-terminal -e "/usr/bin/mplayer /home/eugene/Music/alarm.ogg"
amixer set Master 30%