Introduction: Musical Computer-based Alarm Clock

As a college student I don't get enough sleep, my bed is warm and comfy, and I don't like getting out of it in the mornings.

I typically - as I'm sure many of you do as well - set an alarm (or two) on my phone and on a regular, plugs-into-the-wall alarm clock to get up in the morning. I don't like having to do that. The sounds are annoying, and can be easily silenced which lets me go back to sleep and be late for classes.

This instructable is a documentation of how I set up my computer to wake me, on a very programmable schedule, by playing music and ensures that I am awake. All 3 scripts are included as text files in the last step.

Note: I'm not going to explain anything more than the bare basics of the commands I used. I am assuming you have a basic familiarity with linux, and/or know how to use google.

Step 1: Background and Planning

I have a netbook and a large (17" behemoth) laptop. Both dual boot Ubuntu 12.04 LTS and Windows 7 Home Premium. As my netbook travels with me everyday the laptop is essentially a desktop, complete with second monitor, and external mouse and keyboard. My laptop primarily runs Ubuntu while my netbook typically runs Windows.

I looked through many forums trying to figure a way to schedule a musical alarm and learned about cron, "the time-based job scheduler in Unix-like computer operating systems" (Wikipedia). What I also saw was that many people were scheduling a media player to begin a playlist of files on their computer. I didn't think I'd be able to pick just a handful of songs that I'd want to wake up to every day, and I didn't want to install any new programs or fiddle with settings of pre-existing ones. There had to be another way. An easier way. Since I mostly use Pandora for music listening, and remembered the "gnome-open" command, I decided on Pandora.

Note: If anyone finds a very similar forum posting on by kwrogers, that's me. I didn't copy this from kwrogers, I am kwrogers.

OK, now on to the part you actually wanted to read.

Step 2: Creating the Alarm Script

NOTE: Since I've already done all of this, I've reproduced the steps within ~/bin to provide images.

Open terminal and navigate to a place you'd like to keep the scripts. I created a new directory called bin: 
~$ mkdir bin
Once there, create and edit a new file using whichever is your preferred editing program. I typically use gedit, but nano or any other program could work (and since these recreated steps are done via ssh, I'll be using nano). First we create "pandora_alarm" to launch the alarm, later we will create "cancel_alarm" and another script to launch them both.
$ gedit pandora_alarm

My plan for this alarm was to do a number of things. Namely,
  1. Launch a browser window with pandora
  2. Gradually increase volume to ensure I was awake
  3. Provide some obstacle for me canceling the alarm
To accomplish the first goal I used the "gnome-open" command, which launches my default browser with Pandora.

To accomplish the second goal use the "amixer" command. First I set the master volume to 40%:
amixer sset 'Master' 40%

Then I wanted it to gradually increase in volume. To get the script to pause, use "sleep" and a time frame.
sleep 90s
This pauses the script for 90 seconds. It also works with m (minutes) and h (hours).
Then to increase the volume slightly we could either set a new, specific volume, or simply increase it relative to what it was at. I chose to increase it.
amixer -q sset 'Master' 10%+
This increases the volume 10%. I repeated the sleep/amixer command a few times and then just set the volume to 90% after a few minutes to make me get up for sure.

Finally I needed an obstacle to canceling the alarm, and what better way than locking the screen? This is also particularly effective for me since my usb keyboard is plugged into the second monitor (which acts as a usb hub) and is not active when the monitor is off. This makes me have to turn on the monitor, wait, and THEN log in to cancel the alarm, which means I'm at least mostly awake.

Step 3: Canceling the Alarm

So what happens when I get up before the pandora_alarm script ends? Do I have to suffer from ever-increasing volumes?
No! To cancel the increases in volume I created a second script: cancel_alarm:
$ gedit cancel_alarm
This script requires some form of input from me, and then cancels the pandora_alarm script. The following is the entire script, commented with what each step does.

Step 4: Test Both Scripts

To test these scripts, from the terminal navigate to the directory that contains them.
~$ cd bin
Then attempt to launch the script
~/bin$ ./pandora_alarm
Nothing happens. The scripts have not been granted permissions to be executed as programs yet.
To check if they have permission to be executed:
~/bin$ ls -l
[list here]
To enable executing as a program:
~/bin$ chmod +x pandora_alarm
When you test it out again, viola! It works!

Make sure that you run "chmod +x" for each script that you want to run as a program.

Step 5: Condense Commands

Rather than put two entries into the cron table, I decided I'd rather put in one. To do this, I needed to have another script that launched both pandora_alarm and cancel_alarm. It may sound like extra work, but these are are simple scripts and I was enjoying figuring this all out.
I created a script called "alarm" which simply launches the other two scripts. Once allowing it to be executed as a program, I needed to have it run on a schedule. Enter cron/crontab.

In terminal, type:
$ crontab -e

This will likely prompt you to pick an editor to edit the cron tables if you never have before. Nano is a good one. There will be a whole bunch of commented stuff in here explaining (sort of) how it works. There are many tutorials online elsewhere that detail what cron can do and how to do it, but here I'll just explain what mine does.

The first part details the frequency in the form: min/hour/dayofmonth/month/dayofweek.
So "55 5 * * 1-5" will run at 5:55a every Monday through Friday (1-5)
"~/bin/./alarm" is the command to execute the script, explicitly defined
">> ~/bin/alarm_log.txt" creates and appends an output/activity log, but is not necessary
"./dev/null @>&1" prevents any mail from being sent to notify the action took place

For more information on how to use cron, or any other commands, do a quick google  search. What you find will be more knowledgeable than I am; every command and bit of code used here I either learned for this project or looked up to clarify my understanding of something.

Step 6: Troubleshooting

When you execute a script via terminal, the syntax is "~$ ./scriptpath" but for some reason this eluded me in the crontab. I tried using variations on the full path to the script, placing the period various places before it discovered/realized that it was " home/user/path/./script" or "~/path/./script"
I also tried defining a path within crontab to where the the script could be found, but I couldn't get that to work either.

As the scripts and crontab are now, they work. The alarm launches and functions, but the cancel_alarm script doesn't show up so I have to manually open terminal and pkill the script. This makes sure I am awake, but is inconvenient since I made a script that should do this.
UPDATE: The "alarm" script has been updated here and on my computer, with a small modification which launches the "cancel_alarm" script in a terminal window.

If anyone has any tips or suggestions that'd be great.
Otherwise feel free to download and use these scripts. They are included as text files, so you'll need to remove the ".txt" and enable execution as a program.
Hack It! Contest

Participated in the
Hack It! Contest