slackware button
A SIMPLE CUCKOO CLOCK PROJECT FOR LINUX USING CRON, CRONTAB, AND A FEW SIMPLE SCRIPTS!
by Matt Gilbert March 2011
slack4free.gif

anybrow2.gif

anybrow3.gif

Web pages should be viewable with ANY web browser, and ANY operating system.

anybrows.gif

arachne.gif

Arachne Linux is in the 'proof on concept' stage and really needs some enthusiastic C and/or C++ programmers, click on the arachne button above to learn more!

ms2arach.gif

Got DOS? Bring that old hardware back to life! Try the Arachne browser!

iecrash.gif

I know that some people still use Internet Explorer, and I have to ask myself, 'WHY?'

freespch.gif

archne for dos

y2kbug.gif animated

cuckoo clock picture 1

cuckoo clock picture 2

cuckoo clock picture 3

cuckoo clock picture 5

PREFACE

If you're like me, maybe you grew up with one of those old wonderful european German or Swiss cuckoo clocks in your house. We had one, and I never thought much about it, as it stayed on the same spot between the patio door and the wall mounted rotary dial telephone, it was mounted fairly high up, so it's metal weight pine cones would have plenty of travel space before it hit the floor. Year after year, throughout much of the 60's and a good part of the 70's it would faithfuly do it's thing every hour and on the half hour. First you would hear some mechanisms inside the clock go into action, then one of the pine cones would start to drop as the door on top would open and the little cuckoo bird would proudly come out on his little platform and announce to the entire household, 'BONG.......CUCKOO!......BONG......CUCKOO', with his little head bobbing downward with every cuckoo, we always knew what time it was. The only time he ever failed us was human error. We humans were expected to do something so simple as to pull his two little chains to raise his steel pine cones once every couple of days. Of course from time to time his pine cones would hit the floor and we would hear a very sad and slow 'bonnnnng......cooooo-cooooooooooooo as if the poor little birdy was taking his last breath. Of course, when you'd raise his pine cones he was good as new until the next cycle of neglect. If you were fortunate enough to grow up with one of these little marvels of engineering you would know exactly what I am talking about. Of course nowadays, probably the most attention our clocks require of us is a new AA battery a couple of times a year. Ahhhhhhh, the good ol' days!

INTRODUCTION AND PURPOSE OF THIS PAGE

What is Linux?
skip intro - let's get started!

Let me introduce myself, my name is Matt Gilbert and I live in Houston Texas. I started dabbling in LINUX back in the mid 90's, mostly with small slackware based console distros like zipslack, dragonlinux, phat linux that ran on DOS partitions. My early computer years (After the CoCo TRS-80 years) revolved around DOS, dial up BBS's, lots of DOS games, DOS business apps, and very good, solid and stable software. The good ol' years where everything just 'worked'! However, like many others, I went kicking and screaming into Windows 3.11 and then Windows 98. This was called 'progress'. Programs that didn't work, Blue Screens of Death, shareware, nagware, popupware, surpriseware, crapware, freeware that isn't free at all, trojan virus' and worms in your email, spam, malware and spyware. Endless updates and upgrades, and never mind that all of this cost you money. Not to mention the strong armed tactics used to convince people they needed more memory, a newer computer, and bigger hard drives for all the bloat and crap forced down the unsuspecting conumers throat. I rode on that roller coaster for a long time, but my passion and enthusiasm remained with DOS, BASIC and QBASIC programming, and this relatively newfangled thing called Linux.

Linux was a very foreign and alien thing at first, it wasn't well known or used, at least not by many people I knew, and there didn't seem to be a lot of ready to use easy to install software. DOS was like a favorite pair of slippers you could walk around all day in, Linux just didn't seem obvious or intuitive, at least not at first. I would boot it up and stare at the command prompt and wonder, 'hmmm, what should I do?" In DOS, you might download a zip file from a BBS and simply unzip it and follow a few menu driven configuration instructions and you were good to go. Linux was a completely different animal. It came in different flavors, or distros, which already had much of what you might need. Still, the learning curve was, and still is, at least for me, rather steep, I by no means consider myself any kind of 'guru', but I've been totally fascinated by Linux, stuck with it, and the one thing I can say hands down is that it is logical. You can tinker with it, take it apart, put it back together, rearrange, it, come up with your own ideas. So, despite the learning curve, that is what has held my fascination, and interest. It's amazing how you can find 10 different ways to do the same thing - still, the learning was slow, and I was comfortable in DOS and Win98, it did what I needed on a day to day basis. If Windows was my wife, then Linux was my mistress, but that was about to change.

My full time conversion to linux was around 2006 when my frustration with windows came to a head. I had spent one weekend tracking down and getting rid of a BHO (Browser Hijacker Object), courtesy of CoolWebSearch that kept respawning itself, it would not let me set my home page, and sometimes redirected to strange pages and brought my computer to a horribly slow crawl. I think I got a few gray hairs figuring that one out. I remember it well, It was called 'about:blank' and I think I got it from a music website when I clicked on a link for something 'free'. In all fairness, I was asking for it, as I KNEW I should not be using Internet Explorer with Javascript and Active-X turned on. This miserable piece of malware and spyware would simply not go away, as it put hidden system files all over the place that would respawn every time I rebooted, you can read about it here. Then wouldn't you know it, the very next weekend when setting up a router somehow the tcp/ip stack got completely FUBAR-ed, since I had Slackware 10 installed on another partition, I was sort of 'forced' to use it, at least for internet, and the more I did, the more M$ Windows became a distant memory in my rear view mirror. I've never looked back. Anything and everything I use to do in DOS and Windows I've found a way to do it in linux. I'm not saying it was quick, or fast, or easy, but it has been very worthwhile, and how can you put a price on the freedom to do things your way? DOS and I are friends for life, but Windows and I have pretty much gone our own separate ways.

As far as this audio 'cuckoo clock' project goes, I was trying to remember how in the world I ever even came up with this idea in the first place. It started a few days ago when I noticed my system clock was several hours slow. About a week ago I had the same problem. I got very annoyed with this for a couple of reasons. One, I figure it's not good to be putting the wrong time/date stamps on files. In fact at one time it was about 16 hours slow, which meant there were times when it not only put the wrong time on my files, but also the wrong date. Secondly, I just figured there is just no good excuse or reason for having a linux machine with it's time having to be reset at such frequent intervals. A little mental backtracing has led me to what I believe was the cause, On both occaisons, I had rebooted my system to either do something in another OS or to try a live linux CD. I normally leave my computer on 24/7 so apparently what had happened was that a live CD had farkled up my time or my hardware clock was off, and of course when linux boots, it's system clock is initially set by the hardware (BIOS) clock. I decided it was time to do something about it.

So although this page is about the cuckoo clock, I feel that sharing my discoveries my help others, so we'll go over that first, briefly. With little research (googling), I found out that it is very easy to set your system clock to the www.ntp.org servers:


(at the root prompt)

# ntpdate pool.ntp.org  [enter]
7 Mar 22:05:37 ntpdate[4747]: adjust time server 204.9.54.119 offset 0.051649 sec


Well, I figured, that's all fine and dandy, if I happen to notice my clock is off, but wouldn't it make better sense to make sure your system clock is set correctly at boot time? That's easy too!. Simply edit your rc.local file. You'll find it in /ect/rc.d on most 'pure' UNIX, Linux, and BSD type systems. Think of your rc.local file as the equivalent of a DOS autoexec.bat file, it's really just a script, and you can put things in it you want your computer to do at start up.


(at the root prompt)

# cd /etc/rc.d
# jstar rc.local

then add these two lines:

# the next line should set the time via ntp
ntpdate pool.ntp.org

Okay, so far so good. That means that when you boot, your system time will be synched with the servers at ntp.org which are probably set by an atomic clock and accurate to a couple trillionths of a second per 1 gazallion millenium. Then I read about something called clock drift, which means that on some computers, the clock may gain a little, or lose a little, resulting in your system time being innacurate. Since my system runs 24/7 I figured it made sense to have the system time checked against the ntp.org time periodically, and adjusted if necessary. I knew of, but had very little experience with cron and crontabs, so I looked into it as a possible solution for my problem, and at some point I realized how cool it would be to have it do other tasks for me automatically, such as perhaps playing a gong or a cuckoo wav file at the top of the hour.

Now I'm not saying the way I went about this is the best way or the only way. The world of Linux has been, and continues to be a wonderful and enlightening experience for me. I feel like a big kid with a giant box of tinker toys and leggos, linux let's you make your own choices and do things your own way, so I just kind of fell into what seemed like a logical sequence and followed it through, I hope you will have fun with this project as well!

In addition to the cuckoo clock hourly sounds, I have some other cool sounds I have going on at the top of the hour, the half hour, and a little 'ding' sound at quarter after and quarter till the hour, so I'll share that with you as well. I can be lying in bed and pretty much don't have to bother looking at the clock and always know what time it is! If I remember hearing 11 cuckoos a little while ago, and heard the 15 minute 'ding', but haven't heard the 30 minute single cuckoo, it's a pretty safe bet is's somewhere between 11:15 and 11:30. I don't even have to strain my neck or squint my eyes to look at the wall clock or turn on a light. It's a teriffic project for lazy folks like me!

SKILL LEVEL NEEDED

slackani.gif I think that just about anyone who uses linux should be able to tackle this. Basically if you understand how to work at the CLI prompt and know the basic UNIX style commands you shouldn't have any trouble. You'll need to create directories, download some files, unzip them, and edit those file to suit the particular path to their location on your machine. Pretty basic stuff really. Linux newbies might have a little trouble with this, especiallly if you're fairly new and just coming over from the windoze world. This would be a good project to roll up your sleeves and get your hands a little bit dirty, as most of what's involved really just involves thinking it through and understanind the process. This would be a great time to get out of that point and click GUI interface and do some things at the CLI level. If you haven't done so, you should set your /etc/inittab to runlevel 3 or whatever it appropriate for your machine to boot to a nice clean prompt. Then, at any time you feel the need to point and click you can always start the X server and bring up the GUI with startx. Ubuntu users may have a somewhat challenging time with this, as Ubuntu makes it difficult to see what is 'under the hood'. To me, that kind of defeats the whole Linux experience, if I wanted everything done for me and other people or companies to make decisions about my computer for me then I would still be using Windoze. At any rate, just jump right in, there is nothing really hard about this project.

ABOUT CRON AND CRONTAB (BRIEFLY)

linux small logo.png

I'm not going to present a comprehensive thesis on cron and crontab, as MUCH is written about it elsewhere. I will discuss it for the purposes with which we need though, of course.

First, check the man page to familiarize yourself with it.


# man crontab

I urge you to research cron a little and then come back, but I'll do my best to explain cron in plain everyday english. We are only concerned with cron, crontab, and crond. Cron is the meat and potatoes of the program, crontab allows you to edit the cron tables which carry instructions for cron, and crond is the cron daemon which runs in the background so that cron can check the system clock against the cron tables to see if there are any events to be executed. If there are, it carries them out. That's fairly straightforward, isn't it? I suspect that cron derived it's name from chronology and they dropped the h, as cron is simply a way for the computer to execute certain events at predetermined times. This would make sense in the unix world, with so many servers and other heavy duty industrial needs, ways and means would be needed to do routine tasks such as cleaning out outdated log files, /temp or /tmp directories, running an automated mail schedule, and automatically backing up data, etc etc. If you need your computer to do something at a certain time or date on a regular basis, then cron is 'the man', so to speak.

Take a look in /etc and you will see four cron related directories, cd to cron.daily and in my case there are four files in there. They are executable scripts and cron will run these once daily. Likewise, the other directories,

/etc/cron.hourly
/etc/cron.monthly
/etc/cron.weekly

will run any scripts placed in them at the appropriate time. Let's take a look at what's in my /etc/cron.daily.


westburian@mattsw12:/etc$ ls -d cr*
cron.daily/  cron.hourly/  cron.monthly/  cron.weekly/
westburian@mattsw12:/etc$ cd cron.daily
westburian@mattsw12:/etc/cron.daily$ ls
certwatch*  google-chrome  logrotate*  slocate*
westburian@mattsw12:/etc/cron.daily$

There are four files. Cron will run these once every day. The certwatch file, a script actually, has something to do with checking for updates for certificates. I didn't put it there, but I'm sure it must be important. The logrotate file purges out log files that are no longer needed. I hear it run at 4:40 a.m. every morning. Or rather, I should say I hear my hard drives one by one rattling away doing something. I don't know what slocate does, but I'm sure it must do something important every day as well or it wouldn't be there. Now, the google-chrome file was put there by the fine folks who make the google-chrome browser, which I thought I would try a while back, but didn't work. I believe it checks for updates automatically, too bad it can't update their browser into something that actually works!

It is probably fair to say that this is the proper place to put scripts for system events to happen at certain times. Obviously, you need to be root, or superuser to add, delete, or otherwise modify anything in this area. For the non root user, however, there exists the crontab command, again, short for cron tables. Running crontab -e will bring up an editor, often times the vi editor, which I'm not particularly fond of. I prefer jstar, and I used to use pico and nano quite a bit. GUI users may prefer kedit or similar. Try to stay out of GUI for this project as much as possible. If you're using a distro that by defaults puts you into GUI when you boot, as I mentioned previously, I strongly urge you to edit your /etc/inittab to a run level which will boot you to a CLI prompt. At any rate, running crontab with the following options at the CLI prompt will do the following.

crontab -e [brings up the vi editor]
crontab -l [shows your current crontab file]
crontab -d [deletes your crontab file]


Here is where my crontab files are located. Note that there is a root crontab file, it has entries that are specific for running the /etc/hourly.cron and other files we discussed above, and note my user file, westburian.

root@mattsw12:/var/spool/cron/crontabs# ls
root  westburian 

You now know basically all you need to know to understand what we will do next, and that is simply create and modify our crontab files. I've done all the hard work so all you will need to do is copy/paste my user crontab file, you'll then create a directory with which to place the scripts I've written, and you'll correct the paths specific to your machine.

LET'S ROCK N ROLL - CREATE REQUIRED DIRECTORIES

slackware is not normal banner You will need to create two directories under your username. You'll note that my machine name is mattsw12 and my username is westburian

~$ mkdir cuckoo
~$ mkdir wav

here is how mine appear

westburian@mattsw12:~$ ls -d cuck*
cuckoo/

westburian@mattsw12:~$ ls -d wav*
wav/

With your directories created, you now only need to download the files and do a few simple tweaks with the paths and make some choices on your preferences.

slackware see the light banner

FILES YOU WILL NEED

You will need these files.

cuckooscripts.zip

download those into your ~/cuckoo directory and unzip them.

next, grab these

miscwavfiles.zip

download those into your ~/wav directory, and unzip those as well.

CONFIGURING AND TESTING THE CUCKOO SCRIPTS

Now, with the cuckoo script files unzipped into your ~/cuckoo directory, we can do some simple tests to make sure they will work on your system. I use the 'play' command, which is SoX, or the Swiss Army Knife of Audio players. You will need to have that on your machine if you wish to use it as I have, if you don't have it, you can get it, or use another audio player of your choice. Here is how the files look on my machine in my ~/cuckoo directory.


westburian@mattsw12:~/cuckoo$ ls
cuckoo.wav    cuckoo11.sh*  cuckoo3.sh*  cuckoo6.sh*  cuckoo9.sh*
cuckoo1.sh*   cuckoo12.sh*  cuckoo4.sh*  cuckoo7.sh*  time.cron*
cuckoo10.sh*  cuckoo2.sh*   cuckoo5.sh*  cuckoo8.sh*  westburian*

Now, let's take a look at what is in file cuckoo3.sh*. This file when either summoned by cron or run by you will cause the cuckoo.wav file to play 3 times in succession, appropriate for either 3 a.m. or 3 p.m. Ditto for all the other cuckoo .sh files.


# rem by matt for 3 cuckoos
play ~/cuckoo/cuckoo.wav
play ~/cuckoo/cuckoo.wav
play ~/cuckoo/cuckoo.wav

To test, type ./cuckoo3.sh at the prompt, if all is well, you will hear 3 cuckoo's and your screen will look like the following, more or less


westburian@mattsw12:~/cuckoo$ ./cuckoo1.sh

/home/westburian/cuckoo/cuckoo.wav:

  Encoding: Unsigned PCM
  Channels: 1 @ 8-bit
Samplerate: 22050Hz
Replaygain: off
  Duration: 00:01.16

100%  00:01.16 [00:00.00] of 00:01.16 Out:25.6k [!=====|=====!]       Clip:0
Done.
westburian@mattsw12:~/cuckoo$

Okay, for that example, I just ran cuckoo1.sh for purposes of clarity. Assuming that you hear cuckoo's through your speakers, then all is well and we are ready to move on.

Note the file named 'westburian' in your ~/cuckoo directory. That is my crontab file which you now need to make yours. Let's take a look at what's inside it.


15,45 * * * * play /home/westburian/wav/DING.wav
30 * * * * play /home/westburian/wav/cuckoo2.wav
0 1 * * * /home/westburian/cuckoo/cuckoo1.sh
0 2 * * * /home/westburian/cuckoo/cuckoo2.sh
0 3 * * * /home/westburian/cuckoo/cuckoo3.sh
0 4 * * * /home/westburian/cuckoo/cuckoo4.sh
0 5 * * * /home/westburian/cuckoo/cuckoo5.sh
0 6 * * * /home/westburian/cuckoo/cuckoo6.sh
0 7 * * * /home/westburian/cuckoo/cuckoo7.sh
0 8 * * * /home/westburian/cuckoo/cuckoo8.sh
0 9 * * * /home/westburian/cuckoo/cuckoo9.sh
0 10 * * * /home/westburian/cuckoo/cuckoo10.sh
0 11 * * * /home/westburian/cuckoo/cuckoo11.sh
0 12 * * * /home/westburian/cuckoo/cuckoo12.sh
0 13 * * * /home/westburian/cuckoo/cuckoo1.sh
0 14 * * * /home/westburian/cuckoo/cuckoo2.sh
0 15 * * * /home/westburian/cuckoo/cuckoo3.sh
0 16 * * * /home/westburian/cuckoo/cuckoo4.sh
0 17 * * * /home/westburian/cuckoo/cuckoo5.sh
0 18 * * * /home/westburian/cuckoo/cuckoo6.sh
0 19 * * * /home/westburian/cuckoo/cuckoo7.sh
0 20 * * * /home/westburian/cuckoo/cuckoo8.sh
0 21 * * * /home/westburian/cuckoo/cuckoo9.sh
0 22 * * * /home/westburian/cuckoo/cuckoo10.sh
0 23 * * * /home/westburian/cuckoo/cuckoo11.sh
0 00 * * * /home/westburian/cuckoo/cuckoo12.sh

This one should be fairly easy to figure out how it works and what it does now that you've made it this far. The first line plays DING.wav on the quarter hour, the second line plays a cuckoo sound on the half hour, and the rest are called corresponding to their hours.

You will of course, need to go through it line by line, and change out the occurances of westburian with your user name so that the correct paths will be specified for 'play' to find them. Then you will need to save the file, you will cp (copy) it as a file with your username, and then finally copy that to /var/spool/cron/crontabs directory, as I showed you earlier where the crontab files 'live'. First, navigate to where you downloaded and unzipped the cuckooscripts.zip in ~/cuckoo and with your favorite plain text editor, open up the file westburian, and change the path names accordingly, let's say your username is bizzybee, make changes as such:

15,45 * * * * play /home/westburian/wav/DING.wav
30 * * * * play /home/westburian/wav/cuckoo2.wav
0 1 * * * /home/westburian/cuckoo/cuckoo1.sh
0 2 * * * /home/westburian/cuckoo/cuckoo2.sh
0 3 * * * /home/westburian/cuckoo/cuckoo3.sh

would be changed to:


15,45 * * * * play /home/bizzybee/wav/DING.wav
30 * * * * play /home/bizzybee/wav/cuckoo2.wav
0 1 * * * /home/bizzybee/wav/cuckoo/cuckoo1.sh
0 2 * * * /home/bizzybee/wav/cuckoo/cuckoo2.sh
0 3 * * * /home/bizzybee/wav/cuckoo/cuckoo3.sh

Do that with all the the entries, note there are 24 of them which will run a script that corresponds to that hour. Once that is done, save the file, which is still my username 'westburian'. Use the cp (copy) command to copy it as a new file with your username, then copy it to the crontabs directory. You will need to be root to do this.

cp westburian bizzybee
cp bizzybee /var/spool/cron/crontabs

CONGRATULATIONS!!!!

ENJOY YOUR NEW CUCKOO CLOCK!!!

The rest of your household will now be looking everywhere wondering where you've hidden the cuckoo clock! Of course, I'm sure you realize you can replace your home ~/cuckoo/cuckoo.wav file with something else of your choosing, a grandfather clock chime.wav or a gong, or whatever. Simply rename your .wav file to cuckoo.wav and you'll be in business :)

Now, you can also set up some other sound events to happen at the top of the hour if you wish. First, in root, copy the time.cron file (it's in your ~/cuckoo directory where you unzipped it) over to /etc/cron.hourly. Next, download the miscwavfiles.zip to your home ~/wav directory and unzip it if you haven't already done so.

At the time I configured time.cron I had the corresponding .wav files in my ~/wav directory, but I converted those .wav to mp3 files and those are what is in the miscwavfiles.zip file. No problem, simply edit your time.cron file to play the mp3's of your choosing at the top of the hour. You can do them individually, the hard way, one line for each sound, as I did, or you can do it with a simple command, such as 'play ~/wav/*.mp3 for example. I actually ended up using 'mpg123 /home/westburian/wav/*.mp3 and it's worked fine. You may need to edit your root crontab for when you want your hourly events to occur. By default my root hourly setting was 47 minutes after the hour. I actually set mine for 59 minutes so that at my mp3's will start one minute before the hour, and then my cuckoo scripts plays at exactly the top of the hour. That way 'Big Ben' and some other sounds get a head start before the cuckoo clock. When growing up, we also had early 1900's Thomas Mantel clock, it was always a little slow, and the cuckoo clock was always a little fast, we'd constantly adjust them but they were never synched! So having your user's cuckoo clock sound at exactly the hour and slightly preceded by the hourly events call by the root /ect/cron.hourly does, in my opinion, add a sense of realism.

Remember, your /etc/cron.hourly/time.cron file's first line will update your system clock every hour with the ntp.org servers, assuming you have an always on internet connection. So you will definitely have the most accurate clock on the block!!!

My next project I may show how you can use minicom as a caller ID so you can not only see who's calling, and what calls you've missed, but you'll have a permanent record of who called, comes in handy for when you can't find a 'lost' phone number!

ENJOY!!!!

picture of a traditional european cuckoo clock

The above looks very much like the cuckoo clock I remember growing up with.

slackware logo cd cover

Discover the power and freedom of linux! I recomend Slackware! Keep the windows shut and the internet open!

 tux with dobbs picture

Slackware is not only the oldest surviving linux distro, but it's vanilla approach means that you customize it to fit your needs. This approach is what has given it it's legendary reputation for stability.

VFW ribbon yellow and blud support our troops

Support our Troops, click on the ribbon to find out how.

ascii animated walking figure

ASCII man says, 'There are 10 kinds of people in the world, those who understand binary, and those who don't, and if you ASCII me no questions, I'll tell you no lies'
Confucious say 'no matter where you go, there you are'.

slack madman logo cartoon I don't do windows.gif linux logo
Matt Gilbert
COMMENTS? QUESTIONS?
IDEAS? FLAMES?
CONTACT ME

HOME   BBQ   PIZZA   PHOTOS   MUSIC ROOM   VETERANS