My nightly routine for years has involved falling asleep to music. The few times in my life (hospital, sleep clinic, basketball camp) that I didn’t have music to fall asleep to can be counted as some of the most miserable of my life.

What started as a radio, became a boom-box playing cassettes. Then cassettes gave way to CDs. Recently (as in the past 5 years) I’ve come to love internet radio. For a long time I had an old windows laptop that wouldn’t hold a charge and had to be plugged in serving as the nighttime jukebox.
A Little Night Music
Unfortunately, I had an idea one day (my wife hates when she hears that) and that idea ultimately destroyed the old laptop. Meh. No problem. I substituted my netbook. Better CPU, more memory and it ran linux!! I was able to script a cron job to turn the machine off (sort of a sleep timer) automatically at different times during the week and on weekends. It worked great!

Eventually, I started just leaving my netbook at home. That made me sad. The little netbook was good at a lot of things. Being stuck as a dumb jukebox seemed like such a waste.

Until now.

I recently came into possession of an old desktop machine that I stripped for parts. One of those parts was a D-Link wireless PCI card.

Last night I installed the wireless card in Pugsley (another low-power, headless linux server that lives in my living room). After screwing around for a while figuring out how to get a wireless connection from the command line, I moved Pugsley to the bedroom and set him up next to my nightstand.

I installed VLC, because I know it can handle streaming audio and will run from the command line. I stopped by .977 and grabbed some streaming addresses for a couple music channels (80′s and Smooth Jazz!) and set up vlc to play the streams from a script. Cool.

Now to schedule the music. I knew I wanted to have Pugsley start the music at 9:00pm every night (jazz during the week, 80′s on weekends) and shut the music down in the mornings (4:00am during the week, 11:00am on weekends)

First I created a cron job from the root shell to stop the music. That was an easy ‘killall’ command:

# Nighttime music
00 04 * * 1-5 killall vlc
00 11 * * 0,6 killall vlc

Then I wrote the cron job to start the music (depending on the day) from my user shell (VLC doesn’t like being started by root):

# Turn on Night Music (every night @ 9:00pm)
# Friday & Saturday
00 21 * * 5-6 /home/chuck/scripts/80s.sh
# Sunday Thru Thursday
00 21 * * 0-4 /home/chuck/scripts/jazz.sh

Now I just needed to write the scripts to play the music. While the script is an easy one liner, the real trick was specifying a dummy interface to VLC. Without an interface (such as the terminal) VLC will die with an error. So calling it from within a cron job won’t work.

Googling around I found this forum entry

So I wrote my scripts:
80s.sh:

1
2
#!/bin/sh
vlc --volume 30 -I dummy http://www.977music.com/tunein/web/80s.asx

and jazz.sh:

1
2
#!/bin/sh
vlc --volume 30 -I dummy http://www.977music.com/tunein/web/smoothjazz.asx

So now, every weeknight, the computer starts up some smooth jazz and and shuts it down before my alarm goes off for work. Every weekend, it serenades us with 80′s music and shuts it down the next morning before noon! Plus, I got my netbook back! Awesome.

Now, to get some sleep.

Tags: , , , ,