Create a semi-failsafe automated webcam system

Jun 07, '04 12:14:00PM

Contributed by: robg

I'm putting this in the "Reviews" category, because I can't really decide where it might best fit. We have a G3/500 iBook that hasn't been getting much use lately, so I thought I'd try it as a webcam for while, just to put it to some sort of use (I hate selling off my older Macs!).

Using a combination of built-in programs, OS X features, and EvoCam, our little iBook is now a fully-automated, somewhat failsafe, webcam serving machine. There's really nothing new in this hint/review, just a combination of things that have been covered here before all put to use in one solution. So read the rest of the hint if you'd like to see what I did...

First, none of this would be possible without EvoCam, a previous PotW winner and a great webcam application. EvoCam takes care of most of the hard part of the setup, as its feature set includes these very useful features:

  1. Built-in webserver for live camera functionality
  2. Snap a still image every certain number of seconds and write it to disk
  3. Create a time-lapse QuickTime movies from the stills automatically
  4. Operate according to a schedule
  5. Add text and graphic overlays on top of the webcam image
I used all of these features when we set up the camera -- for now, I'm using the iSight camera, but EvoCam will work with nearly any camera that QuickTime supports. The EvoCam server starts at 5:45am each day, and shuts itself down at 6:00pm (it's an outside cam, so not much to see when it's dark!). During the day, it snaps an image every 30 seconds and writes it to the iBook's drive, and builds the QuickTime time-lapse movie while doing so.

The second part of the solution was trying to failsafe the camera, so that I wouldn't have issues with it going offline during the day. The first stop was the Energy Saver preferences panel. First, I made sure the iBook didn't fall asleep on the job by setting the Sleep section to "Never," (but the screen dims after one minute). Next, on the Options tab, I enabled the "Restart automatically after a power failure" to take care of temporary power outages. I also created a new "Webcam" user for this project, and put Evocam in that user's startup items folder, and enabled automatic login. So after a power outage, the machine should reboot, login the Webcam user, and launch EvoCam. I haven't tested this yet, but it should work :).

I then switched to the scheduling tab of Energy Saver, and told the iBook to wake up every day at 5:40am (five minutes before the camera starts snapping pictures), and to put itself in sleep mode at 8:00pm each evening (so I have time to deal with the day's image captures, transferring them to our main Mac -- 1,500+ images per day fills that little hard drive quite quickly!).

These steps took care of the "easy" part of the project: the machine would now wake/sleep itself and recover from a power outage. Next, I wanted to make sure EvoCam was always running. It's a great little app, but I have had it unexpectedly quit on occasion, but I couldn't ever figure out the cause. Thanks to hayne in this thread on the forum site, and some digging through older macosxhints tips, I cobbled together a solution. First, I had to disable the Crash Reporter, so that those lovely "Application X has crashed..." dialog boxes no longer appeared onscreen. This was handled with a quick trip to the Terminal and this command:
defaults write com.apple.CrashReporter DialogType none
There are other choices for the CrashReporter setting; they're covered in this hint. Next, it was time to figure out how to relaunch EvoCam if it crashed, and to hopefully create a rudimentary log of the events, just so I could see that my efforts paid off. Between hayne's example and the other scripts on the site, here's what I came up with. I saved this in the Webcam user's ~/bin folder, and made sure it was executable (chmod 755 stayup):
#!/bin/sh
#Script name: stayup
ps ax | grep EvoCam | grep -v grep
if [ $? = 0 ] ; then
  #it's up, do nothing
  echo "it's running" > /dev/null
else
  #not up, launch it
  date >> /Users/Webcam/Desktop/evocam_crash.txt
  echo "EvoCam restart successful" >> /Users/Webcam/Desktop/evocam_crash.txt
  echo "-------" >> /Users/Webcam/Desktop/evocam_crash.txt
  open -a EvoCam
fi
I'm sure there are probably much more efficient ways of accomplishing this task, but this one works for me. For the real beginners out there, I'll try my best to explain what this script does (though I'm mostly a shell scripting beginner myself, so apologies in advance for mistakes). By itself, this script isn't much good. It runs, but it needs to run automatically every so often, otherwise I'd have to be sitting here running the command. That's where cron enters the picture. cron can execute commands at predefined intervals, which is exactly what I needed. I won't cover crontab again in detail, as there have been quite a few hints on it here -- this one provides a general overview and explanation of how it works. The particular crontab lines for my script are:
#min     hour     mday     month     wday       command
*/1      *        *        *         *          /Users/Webcam/bin/stayup > /dev/null
This command executes my stayup script once per minute, all day long. If EvoCam ever crashes, it will be down at most for 60 seconds, and it will then restart, and log the event in my log file. While this may put a bit of load on the CPU, this machine isn't doing anything else at all at the moment (and when I have been using it while the cronjob is active, I haven't noticed any slowdowns).

As I stated up front, there's nothing new or dramatically exciting in this hint, but hopefully it shows you how you can use the GUI and Unix sides of OS X together in a meaningful way. I'm sure the script can be improved upon (I really don't do a lot of shell scripting), but hopefully this is enough to get you started if you're interested in a similar project.

Comments (14)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20040607121455607