Created
June 22, 2017 19:20
-
-
Save ManuelBehrendt/a14ffc3e8264f9ec30238bdec41b9c34 to your computer and use it in GitHub Desktop.
Email notification of your finished program or due to interruptions. Sends Runtime information and screen output.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# --------------------------------------------------------------------- | |
# This script measures the runtime of your post-processing program | |
# and sends an email notification with the information: | |
# - Start time | |
# - End time | |
# - Runtime | |
# - Your program screen output or interruptions. | |
# Creates an individual logfile. | |
# | |
# Manuel Behrendt - since August 2014 | |
# --------------------------------------------------------------------- | |
# Only fill in MYEMAIL and start script: | |
# type in command line: ./notifyme.sh "python myprogram.py" | |
# or: ./notifyme.sh ./myprogram.o | |
# --------------------------------------------------------------------- | |
# email settings | |
SERVER_NAME=$HOSTNAME | |
SENDER=$(whoami) | |
MYEMAIL="email@domain.com" # fill in the recipient email address | |
SUBJECT="Finished [$1] from $SENDER on server $SERVER_NAME" | |
INTERRUPTION="Interrupted [$1] from $SENDER on server $SERVER_NAME" | |
# define function | |
convertseconds() { | |
d=$(($1/(60*60*24))) | |
h=$((($1/3600)%24)) | |
m=$((($1/60)%60)) | |
s=$(($1%60)) | |
printf "%02d days, %02d:%02d:%02d\n" $d $h $m $s | |
} | |
# remember start time and create individual logfile | |
STARTTIME=$(date +%s) | |
STARTTIME_formatted=$(date -d @$STARTTIME) | |
LOGFILE="log_"$STARTTIME".out" | |
# sends email with informations when process received a signal: | |
# -termination -hangup -user interruption | |
trap '{ ENDTIME=$(date +%s); \ | |
ENDTIME_formatted=$(date -d @$ENDTIME ) | |
i=$(($ENDTIME - $STARTTIME)); \ | |
RUNTIME_formatted=$(convertseconds $i); \ | |
echo "Start: " $STARTTIME_formatted; \ | |
echo "Interruption: " $ENDTIME_formatted; \ | |
echo "Runtime: " $RUNTIME_formatted "(h:m:s)"; \ | |
echo ""; \ | |
echo "Logfile: " $LOGFILE; \ | |
echo ""; \ | |
cat "$LOGFILE"; } | \ | |
mail -s "$INTERRUPTION" $MYEMAIL' TERM KILL HUP | |
#==================================================================== | |
# execute my program $1 and write screen into logfile | |
echo "Logfile: " $LOGFILE | |
$1 > $LOGFILE 2>&1 | |
#==================================================================== | |
# calculate runtime | |
ENDTIME=$(date +%s) | |
ENDTIME_formatted=$(date -d @$ENDTIME ) | |
i=$(($ENDTIME - $STARTTIME)) | |
RUNTIME_formatted=$(convertseconds $i) | |
# send notification of the time information and the logfile | |
OUT=$(cat "$LOGFILE") | |
mailx -s "$SUBJECT" "$MYEMAIL" <<EOF | |
Start: $STARTTIME_formatted | |
End: $ENDTIME_formatted | |
Runtime: $RUNTIME_formatted (h:m:s) | |
Logfile: $LOGFILE | |
$OUT | |
EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment