#!/bin/bash # if [ -z "$RECALL_HIST" ] then export RECALL_HIST="`history 1 | awk '{ print $1 }'`" if [ -z "$RECALL_HIST" ] then if [ -s "$HISTFILE" -a -r "$HISTFILE" ] then export RECALL_HIST=$[`wc -l < $HISTFILE` - 2] else export RECALL_HIST=1 fi fi fi export RECALL_INTERVAL=${RECALL_INTERVAL:-30} export RECALL_DUMP=${RECALL_DUMP:-~/.TR/dump} export RECALL_TTY="`tty | sed 's;/dev/;;'`" # # Alarm generator. function recall_alrm () { # # Directories sometimes disappear, so post # the pending ALRM from / using a subshell. ( cd / echo "kill -ALRM $$ 2>/dev/null # $RECALL_TTY" | \ at now + $[$RECALL_INTERVAL+($RANDOM/8192)]minutes ) 2>/dev/null } # # State dumper. function recall_dump () { # # Restart for next time. if [ "$RECALL_EXIT" != yes ] then recall_alrm else echo recall_dump: final. fi # # We only dump if there's been activity. newhist="`history 1 | awk '{ print $1 }'`" if [ $newhist -ne $RECALL_HIST ] then # # The dir may not be ready yet. thishost="`echo $HOSTNAME | sed -e 's/\..*$//'`" workdir="$RECALL_DUMP"/"$thishost"/"$RECALL_TTY" if [ ! -d "$workdir" ] then mkdir -p "$workdir" chmod 700 "$RECALL_DUMP" fi timestamp="`date '+%Y~%m~%d-%H:%M:%S'`" # # Bury this background invocation THREE shells deep (triple paren) # to avoid the job being seen as a product of the topmost shell: # Note placement of backgroundifying `&'. ( ( ( echo --pid=$$ echo --tty="$RECALL_TTY" geometry="`stty -a < /dev/$RECALL_TTY | grep columns | sed -e 's/^.*rows \([0-9]*\); columns \([0-9]*\).*$/\2x\1/'`" echo -geometry "$geometry" echo -e '\n--dirstack-begin' builtin dirs -l echo -e '--dirstack-end\n\n--history-begin' history $[$newhist-$RECALL_HIST] echo -e '--history-end\n\n--environment-begin' RECALL_TIMESTAMP="$timestamp" printenv | sort echo -e '--environment-end' if [ "$RECALL_DIRLIST" ] then echo -e '\n--changes-begin' ( find . `echo "$RECALL_DIRLIST" | tr : ' '` -xdev \ -maxdepth 2 -mtime -1 -type f -print | xargs ls -ld ) 2>/dev/null echo -e '--changes-end' fi ) < /dev/null > "$workdir"/"$timestamp" case "$RECALL_MAIL" in 1 | [Yy] | yes | YES | on | ON) mail -s "Total Recall: $timestamp $HOSTNAME $RECALL_TTY" "$USER" < "$workdir"/"$timestamp" rm -f "$workdir"/"$timestamp" ;; *) ;; # nothing more to do. esac ) & ) export RECALL_HIST="$newhist" fi } # # We will pick up on this with alarm signals and on exit. trap "RECALL_EXIT=yes recall_dump" EXIT trap "recall_dump" ALRM # # And we'll start the process now. recall_alrm