From 36460e42a406faf9d6e37607e644309ab2339344 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Tue, 13 Mar 2012 08:19:31 -0400 Subject: [PATCH] Add monfold script to version control and add bash tag to inotify post. --- posts/inotify.mdwn | 3 +- posts/inotify/monfold.sh | 77 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 posts/inotify/monfold.sh diff --git a/posts/inotify.mdwn b/posts/inotify.mdwn index 1680497..6987415 100644 --- a/posts/inotify.mdwn +++ b/posts/inotify.mdwn @@ -5,7 +5,8 @@ I just discovered `inotify` and it's command-line incarnation [inotify-tools](http://packages.debian.org/lenny/inotify-tools) on Debian). Now I can watch the data come in from home. If only I had the laser- and photodiode-alignment screws motorized... Anyhow, -checkout [my script](/~wking/code/#monfold). +checkout [[monfold.sh]]. +[[!tag tags/bash]] [[!tag tags/linux]] [[!tag tags/programming]] diff --git a/posts/inotify/monfold.sh b/posts/inotify/monfold.sh new file mode 100644 index 0000000..3016d1b --- /dev/null +++ b/posts/inotify/monfold.sh @@ -0,0 +1,77 @@ +#!/bin/bash +# +# W. Trevor King, 2009. Released into the public domain. +# +# Monitor an unfolding data directory and output a small png of the most +# recent unfolding curve +# +# usage: monfold +# e.g. : monfold ~/rsrch/data/unfold/20090331 +# +# Requires the inotify-tools package if you're running Ubuntu +# +# Running something along the lines of +# qiv --watch $OUTFILE # run qiv tracing the image file +# is a useful way to monitor the generated image without constantly +# grabbing the upper window. + +if [ $# -ne 1 ]; then + echo "usage: monfold " + exit 1 +fi + +DATADIR="$1" +GPFILE="./monfold.gp" +DATALINK="./monfold.data" +OUTFILE="./monfold.png" +ACTION="scp $OUTFILE einstein:./public_html/rsrch/monfold.png" # upload to web + +echo "set terminal png +set output '$OUTFILE' +plot '$DATALINK' using ((\$1/2**15-1)*10):((\$2/2**15-1)*10)" > "$GPFILE" + +# The way output from the unfolding software works, we get a bunch of +# YYYYMMDDHHMMSS_unfold +# files (which are what we want to be monitoring), along with some +# YYYYMMDDHHMMSS_unfold_ +# files with information we ignore. +# +# The files come out sequentially in time, but are written in multiple chunks +# $ inotifywait -q -m -e CLOSE --exclude '.*unfold_.*' 20090331/ +# 20090331/ CLOSE_WRITE,CLOSE 20090331163023_unfold +# ... +# 20090331/ CLOSE_WRITE,CLOSE 20090331163023_unfold +# 20090331/ CLOSE_WRITE,CLOSE 20090331163044_unfold +# ... +# 20090331/ CLOSE_WRITE,CLOSE 20090331163044_unfold +# Triggering on CLOSE isn't actually a good idea, since our monitoring +# activity involves reading the new file, which produces a bunch of +# CLOSE_NOWRITE,CLOSE events. In the loop below we only look at +# CLOSE_WRITES, and we trigger off the transition from one file to the +# next, e.g. the 23->44 second example shown above, after which we +# process the 23 second data. + +LASTFILE="" +# Multiple close events for the same file as data is written in blocks +while read LINE; do + DIR=`echo "$LINE" | awk '{print $1}'` # has trailing slash + EVENT=`echo "$LINE" | awk '{print $2}'` + FILE=`echo "$LINE" | awk '{print $3}'` + if [ "$EVENT" != "CLOSE_WRITE,CLOSE" ]; then + echo "Error (unexpected event '$EVENT'):" + echo "$LINE" + exit 1 + fi + if [ "$FILE" != "$LASTFILE" ] && [ "$LASTFILE" != "" ]; then + # We must have finished writing to $LASTFILE... + echo "process $DIR$LASTFILE -> $OUTFILE" + ln -s "$DIR$LASTFILE" "$DATALINK" + gnuplot "$GPFILE" + rm "$DATALINK" + echo $ACTION + $ACTION + fi + LASTFILE="$FILE" +done < <(inotifywait -q -m -e CLOSE_WRITE --exclude '.*unfold_.*' "$DATADIR") + +rm "$GPFILE" -- 2.26.2