Alarm clock on Linux (Ubuntu), “geek style”

Read this article in english Diesen Artikel auf Deutsch lesen

Is there a better way waking up, than listening to your favourite music? If you're one of those guys, that leave their computer running even if they go to bed, this post may be useful for you... We'll use a script and cron to create a "really geek" alarm clock...
We'll start creating the script, that'll be responsible for playing the music files and turning up the volume gradually...

So, we'll need to open a terminal and write:

gedit alarm

Where we paste the following script

#!/bin/bash
 
xmms2 clear
xmms2 radd /home/ubuntublog/MusicAlarm/
xmms2 play
 
for ((i = 1; i <= 100; i++)) do
amixer sset Master playback $i%
sleep 5s
done

It's important to replace the folder we specified before (/home/ubuntublog/MusicAlarm/), by a folder that contains music files or other folders with music files in it. xmms2 will look for music files recursively. Analizing the script, you'll notice that we'll need the xmms2 package to be installed, if it's not installed yet:

sudo apt-get install xmms2

Now we'll create a file to stop the alarm, we'll need to open a terminal and write:

gedit stopalarm

And paste into the file the following lines:

#!/bin/bash
 
kill `ps -A | grep "xmms2" | cut -c1-5`
kill `ps -A | grep "alarm" | cut -c1-5`
amixer sset Master playback 60%

To execute them...

We'll need to give both files the permission to be executed:

chmod +x alarm stopalarm

Now we'll only need to configure crontab to execute the alarm script, at the time we want to get up...
Because of the "mighty" of crontab we'll be able to configure more than only one alarm, on different days of the week, and at different times.
So we'll write in a command shell:

crontab -e

If this is the first time we are executing this file, there'll apear a line, which we'll delete, to add the ones we'll really need...

¿How to configure the alarms?

The lines we add, must respect the following format:

Mi Hr Dm My Dw Cm

Where:

Mi: Minutes -> 0...59

Hr: Hour -> 0...23

Dm: Day of the month -> 1...31

My: Month of the year -> 1...12

Dw: Day of the week -> 0...6 | 0 = Sunday, 1= Monday, etc

Cm: Command to execute

We can also use the asterisk "*" as our wildcard.

Here are some examples of configured alarms:

30 17 * * * /home/ubuntublog/alarm
0 8 * * 1 /home/ubuntublog/alarm
0 9 * * 3 /home/ubuntublog/alarm

In this case the alarm is going to start every day at 17:30, every monday at 8:00 o'clock and wednesdays at 9:00 o'clock.

Tipp: In NANO editor we can save the current file pressing F3 and exit the editor pressing F2.

Finally we'll create a direct access on our desktop to be able to stop the alarm. Click on the desktop with right mouse button and choose "create a launcher..."

Create Shortcut

Than we'll complete the following form:

Tipe: Aplication

Name: Stop alarm

Command: /home/user/stopalarm (Path where stopalarm is located, replace user by your username).

The comment is optional as it is the icon, which we steal from http://concepcion.flisol.cl

Once we accept, the icon should appear on our desktop. Shoul look like this one:

Acceso directo apagar despertador

The script stopalarm "kills" the processes "xmms2" and alarms. It also sets volume on 60%

It's not permitted to get bad at ubuntu-blog, if you have to get up early and your computer got shutted down at night... because of an outage or any other reason...

2 Comments so far »

  1. Dorset web design said

    am June 15 2009 @ 8:11 am

    You could schedule a cron job to play an mp3 file at a certain time, but there’s probably a desktop app that does this for you.

  2. hanum said

    am October 15 2009 @ 1:55 am

    a little bit difficult config but it’s a good

Comment RSS · TrackBack URI

Leave a comment

Name: (Required)

eMail: (Required)

Website:

Comment: