#!/usr/bin/perl -w # # $Id: therm_daemon.pl,v 1.1 2004/06/20 23:22:38 gaijin Exp $ # # Minimalist temperature-server daemon. Interrogates USB controller and # appends temperature data to end of the specified log file. A kill signal # of any sort will cause the daemon to exit moderately gracefully. # # Daemonize code liberated directly from the excellent O'Reilly Perl Cookbook. # #------------------------------------------------------------------------------- # # Set $sleep_secs to the time (in seconds) between samples. Anything less # than 10 is overkill. Try 10 or 20 while testing, but you probably want a # minimum of 60 for long-term monitoring (I use 1800 for some larger sensor # networks). # # Set $h_time to "1" if you'd like to have the timestamp logged in normal # "date" format. The default of "0" creates seconds-from-epoch timestamps, # which are useful when feeding the readings straight into a database. # # Set $BIN_DIR to the full path (including a trailing slash) of the # directory where the "puce_controller" binary is installed. # # Set $T_LOG to the location (including filename) where you'd like the # output data written. # #------------------------------------------------------------------------------- # use strict; use warnings; use POSIX; my $sleep_secs = 15; ## Time to wait between samples (in seconds). my $h_time = 0; ## Set $h_time to "1" for human readable ## timestamps (rather than seconds from ## "epoch" count). my $BIN_DIR = "./"; my $BIN = $BIN_DIR . "puce_controller"; my $T_LOG = "/tmp/temperature.log"; my $killed = 0; ## ## Minimal signal handler. ## sub sig_handler { $killed = 1; } ## ## Daemonize. ## my $pid = fork(); exit if $pid; die "Failed to fork: $!" unless defined($pid); POSIX::setsid() or die "Cannot start new session: $!"; $SIG{PIPE} = 'IGNORE'; $SIG{INT} = $SIG{TERM} = $SIG{HUP} = \&sig_handler; $| = 1; open(TLFH, ">> $T_LOG") or die "Could not open: $T_LOG.\n\t$!"; ## ## Take it away, Sam! ## until ( $killed ) { for (`./puce_controller 0`) { chomp; my ($timestamp, $serial, $temp, $ex_temp) = split(/:/); ## Human-readable timestamps? if ($h_time == 1) { chomp($timestamp = ctime($timestamp)); print TLFH $timestamp, " "; } else { print TLFH "Timestamp: ", $timestamp, " "; } print TLFH "Serial: ", $serial, "\t", "Temperature: ", $temp, "C\n"; } sleep($sleep_secs); } close(TLFH); exit 0;