#!/bin/bash # This shell script runs a Matlab script (making use of the JIT compiler in # Matlab >=6.5) detached from the invoking terminal and sends an email # notification upon completion. The email consists of execution statistics as # well as the first 50 lines of code output. e.g., matbg foo.m bar.out # # Author: Joshua V. Dillon; jvdillon AAT purdue DDOT edu # Date: Oct. 17, 2006 # Feb. 24, 2007 # change user parameters as appropriate TO="xxxxxxxx@xxxxxx.xxx" # must set the following to enable in-script alias definitions: shopt -s expand_aliases MATLAB="/usr/local/bin/matlab" MFLAGS="-nosplash -nodesktop" STARTUP="run('/home/jvdillon/.matlab/startup.m')" if [ x"$1" = x"" ]; then echo 'you must specify something for MATLAB to execute' exit fi if [ x"$2" != x"" ]; then if [ x"$1" != x"$2" ]; then FILEOUT=$2 else echo 'you must specify a different output file' exit fi else FILEOUT=/dev/null fi MFLAGS="$MFLAGS -r \"$STARTUP;run('${1%.m}');exit;\"" DISPLAY= HEADER="To: <$TO> From: Matlab <$TO> Reply-To: Do Not Reply Subject: Matlab \"$1\" Completed\n\n" TIMEOUT=`mktemp /tmp/$1.XXXXXX` TIMEFMT=' %E\telapsed wall-time, in [hours:]minutes:seconds\\n %S\t\telapsed (kernel) CPU-seconds\\n %U\t\telapsed (user) CPU-seconds\\n %P\t\taverage CPU load' ( DTSTART=`date` # jvd: apparently this method doesn't utilize Matlab >=6.5 JIT compilier #/usr/bin/time -f "$TIMEFMT" -o $TIMEOUT nohup "$MATLAB" "$MFLAGS" < $1 > $FILEOUT & /usr/bin/time -f "$TIMEFMT" -o $TIMEOUT nohup "$MATLAB" "$MFLAGS" &> $FILEOUT & PID=$! #PIDINFO=`ps -f --no-headers -p $PID` echo -e "PID\t\t" $PID >> $FILEOUT wait $PID DTEND=`date` BODY="$1 (executed as \"$MATLAB $MFLAGS > $FILEOUT &\")\n`cat $TIMEOUT`\n Started:\t$DTSTART\n Ended:\t$DTEND\n" echo -e "$HEADER$BODY\nMatlab Output:\n\n`sed '1,13d' $FILEOUT|head -50|sed -e 's/>/ /g'`" | /usr/sbin/sendmail $TO >/dev/null rm $TIMEOUT )&