Monitoring a Shell Command for Differences in Output

A few weeks ago, we experienced some fluctuations in the electric power coming in to the house. Early in the morning on weekends, the line voltage dropped and recovered repeatedly, causing my two UPSs to freak out. They switched to battery and back over and over until the battery ran dead. The utility company has since corrected the problem, but they called to ask if I’ve seen any trouble since. I can see in logs on the NAS when the NAS’s UPS switches to battery and back, but the NAS doesn’t show the UPS’s voltage. The UPSs can report line voltage, but it doesn’t store that information. Momentary status is available by running apcaccess which sends status to standard output in a format containing one variable per line like this (non-relevant sections omitted):

APC : 001,035,0863
DATE : Sat Sep 28 08:06:36 EDT 2013

LINEV : 120.0 Volts

END APC : Sat Sep 28 08:06:59 EDT 2013

This morning, I wanted to watch the voltage to see how much it’s changing. I can run watch, which will run a command repeatedly and show what values have changed since watch has been running, but it doesn’t show the history of those changes. A little Internet searching came up empty for a utility that will get this job done. So, I wrote this Bash script called watchdiff:


while true; do
	    echo ${DASHES} $(date) ${DASHES}
	    echo "${THIS_COMMAND_OUTPUT}"
	sleep 1

Since I’m looking for changes in the line voltage, I run it like this:
watchdiff 'apcaccess | grep LINEV'
The parameters need to be enclosed in quotes here because otherwise the shell will interpret that as (watchdiff apcaccess) | grep LINEV, which will run watchdiff over the entire output of apcaccess, then grep for LINEV. Since apcaccess includes time in its report, the output is always different.

Lastly, to record this data, I run the command thusly:
watchdiff 'apcaccess | grep LINEV' | tee nas-voltage-report.txt