There are many music players that offer the functionality to wakeup to a song or playlist-- both on Linux or Windows. But with each option I've tried, I've never been really happy with the results. For such a simple task, it always seemed overly-complicated. Also, the main downside I found in using a media player plugin, is that you'll need to have the player running for it to actually work in the morning. Below I describe how to create your own music alarm clock, using only command-line utilities found on most Linux distributions. It uses quite a few different tools, and the tutorial will hit on quite a few different concepts. So, without further adieu...
- The first thing you will need is to create a playlist. I used Rhythmbox, since that's where I store all of my music anyway. Create a playlist with songs you'd like to wake up to. When you've got enough, save it in .m3u format, somewhere where you'll find it later. I put mine in my home directory.
- Next, we'll need to make sure we have all the tools for the job. We'll be using cron to schedule our tasks, amixer to set our volume, and mplayer to finally play our music. To make sure you have each of these installed, issue the following command:
sudo aptitude install cron alsa-utils mplayer
- Next, we need to actually add the scheduled task. First I'm simply going to give you the commands, and I'll explain what's going on afterwards. In a terminal, enter the command:
crontab -e
Note: this will open your default text editor, which if you haven't set it, will probably default to vim. - Go to the end of the page by pressing Shift+G. Then start a new line pressing "o". Once you're there, type in or paste the following line:
30 7 * * 1-5 /usr/bin/amixer set PCM 35\% && /usr/bin/X11/xterm -display :0 -bg black -fg white -e /usr/bin/mplayer -shuffle -playlist ~/.alarm-playlist
- Press "ESC" to stop typing. Then enter the command ":wq" (no quotes) to save and quit. If everything went well, you should see the line:
crontab: installing new crontab
man crontabto learn about crontab.
So first of all, we used "aptitude" to install a few packages from the Ubuntu repositories. This is probably familiar to you, or you may be using "apt-get". They are basically the same, but "aptitude" has a few advantages-- you should switch to using it if you haven't already.
Then, we used "crontab -e". Cron is the name of the task scheduler in Linux, and this command opens up our own personal "scheduled task list". You can always use "crontab -e" to edit your tasks, or "crontab -l" just to view them.
Now, on to that crazy line I had you type in:
30 7 * * 1-5 /usr/bin/amixer set PCM 35\% && /usr/bin/X11/xterm -display :0 -bg black -fg white -e /usr/bin/mplayer -shuffle -playlist ~/.alarm-playlistEach entry in your personal crontab has the following format:
minute hour day-of-month month day-of-week commandSo, in our case, our "minute" is 30, "hour" is 7, day-of-month is * (any), "month" is * (any), "day-of-week" is 1-5, and "command" is... the rest of that. This basically means that we've scheduled our command to execute at 7:30 am on Monday through Friday. Changing these options should be self explanatory. Now, let's pick apart our "command" one part at a time.
/usr/bin/amixer set PCM 35\%First thing to note, is that it's a good idea to use full paths for any command you execute from cron. To find out the full path to a command, use
which {command}In this case, we're using amixer, which is a utility for changing the volume on your computer. I set mine to 35% to wake up to, but you can use anything. Also note here that we can't use simply "35%", because cron uses '%' as a special character. Therefore, we preceed it with '\'.
Next thing to notice is "&&". This essentially strings two commands together-- it won't start the next command until our first one has finished. So, onto our next command:
/usr/bin/X11/xterm -display :0 -bg black -fg white -e ...This is actually another compound command. xterm is another terminal that we are going to launch our music alarm in, so we can easily shut if off in the morning. We set all sorts of parameters to make the terminal look nice, but the important one is following the "-e": that's the command we will run in the new terminal:
/usr/bin/mplayer -shuffle -playlist ~/.alarm-playlistAhhh, finally, this is where we finally play our music. mplayer is a command-line music player with a very basic interface, and easy controls. You can run this line in a normal terminal now to make sure it works. We use the parameters "shuffle" to randomize our playlist, and then "-playlist ..." to tell it what to play. Make sure you change "~/.alarm-playlist" to your own location.
And we're done! At this point you should have a fully-functioning music alarm clock. Now go back and tweak it out with preferences that work for you.
Bonus: Move the alarm clock command to a shell script, and keep increasing your volume every minute or so.
2 comments:
I'm sure it's possible, but how hard would it be to set different alarm times for different days? As an example, Mondays and Tuesdays I need to wake up earlier than the rest of the week, so how would I set those days individually? I guess I could do the whole thing twice, once for the first 2 days and then again for the rest of the week, but that sounds messy.
Also, is there a way to stop the alarms from occurring? Holidays, sick days, vacation, etc. I don't want to be waking up if I don't have to!
Responding to your first question, the easiest way is to simply have to entries. For example, I wake up a little later on Fridays, so I have the following 2 entries:
30 7 * * 1-4 {command}
45 8 * * 5 {command}
To turn off your alarms for holidays or when you leave on vacation, you can simply "comment out" your alarm. Comment lines in your crontab is any line that starts with '#'. So, simply change each line you want to disable to something like:
# 30 7 * * 1-5 {command}
Enabling it again later is only a matter of removing that hash mark.
Also, I haven't had a chance to try it yet, but it looks like there is a package called "gnome-schedule" which is a GUI front-end to cron--cool!
Post a Comment