petak, 6. veljače 2009.

Daily music alarm using cron

This is my first guide and here I will show how to make a daily alarm that will wake up computer from standby and start playing music. The additional feature will be increasing volume level as time passes. I’ve done all this on Ubuntu Dapper Drake but it should work on other distributions too.


First is to create a python script that will update /proc/acpi/alarm
with date and time when to wake up the computer.
Since this is the daily alarm this script will put the tomorrow date and
will be called every time when computer wakes up from standby.

Here is the script code:


import time
import datetime
import sysday = (24*60)*60
tomorrow = datetime.datetime.fromtimestamp(time.time() + day)

#setting wake up time
output = tomorrow.strftime(”%Y-%m-%d”) + ” 06:59:50″

print output
try: alarm = file(’/proc/acpi/alarm’,'w’)
except:
print (’Failed to open “/proc/acpi/alarm”!’)

sys.exit()alarm.write(output)


All this script does is increasing the date in /proc/acpi/alarm. You can run the script to see the output that will be put in the file. If run as a non root user the script will fail to write the line to file, so it will be neccesery to run it each time as root.

In this is example alarm will start in 07:00 so I decided to wake up my machine 10 seconds earlyer (06:59:50).

Next thing is to create cron file that will inform cron when to run the script and start playing music.
Content of alarm.cron file:


0 7 * * * /usr/bin/tomorrow.py
0 7 * * * mpg321 –random /data/Music/*


First line tells cron to run tomorrow.py (above python script) every day in 7:00. The second one starts playing random songs from music folder using mp321 player.

Since the tomorrow.py requires root privileges we will have to use root’s cron. To do this we just use sudo when running crontab:


sudo crontab alarm.cron

To see if the crontab has been updated use:

sudo crontab -l

Additionaly we can make sound volume to increase each minute.
I have done this using amixer. See ‘man amixer’ for more info.
So all you need is to add next lines to your alarm.cron file:

0 7 * * * amixer -c 0 sset Master,0 30%,30%
1 7 * * * amixer -c 0 sset Master,0 40%,40%
2 7 * * * amixer -c 0 sset Master,0 50%,50%

Note that to start the wake up alarm loop you will first have to run the
python script with sudo. You will have to do this each time when the
cron didn’t do it. For example if you forget to leave computer on standby.

I hope you learned something from this little guide.
Your feedback is always welcome.

Testing code preview

Hello, this is just a test of inserting code samples in post...

For now the best solution that I found here is this:

//

It shows some java code I wrote in TopCoder.

All you have to do is paste code from that link above at the end of
your html file. In post I've put my code between PRE tag, and it
works just fine:)

Only problem I had to solve was the < symbol in for loop. It was
recognised as a tag so I had to put whole code inside < !-- comment,
and also I had to put // in front so that it dosen't mess with java code.

To enable python code formatting add following line to html file:



Check here for other available scripts.

Hello universe

dkomlen.blogspot.com has just been lunched in the blogger universe. I plan to write random stuff about programming under linux, Ubuntu and other interesting things. Maybe I’ll try making some tutorials later on.