Created
June 19, 2012 18:37
-
-
Save anonymous/2955793 to your computer and use it in GitHub Desktop.
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
readall () { | |
read all | |
while read line; do | |
all="$all\n$line" | |
done | |
echo -e $all | |
} | |
waitpid () { | |
pid=$1 | |
while true; do | |
kill -0 $pid &> /dev/null | |
if [ $? -ne 0 ]; then | |
return | |
fi | |
sleep 60 | |
done | |
} | |
emailwhendone () { | |
# There are four ways to run this: | |
# $ emailwhendone PID | |
# This traces a PID and emails you when it is no longer found in the list | |
# of active processes. | |
# | |
# $ command && emailwhendone | |
# Only send an email when command is successful. | |
# | |
# $ command ; emailwhendone | |
# Send an email even if command fails | |
# | |
# $ command | emailwhendone | |
# Send an email where the message body is the last 100 lines of output from | |
# command. | |
if [ $# -eq 1 ]; then | |
echo "tracing pid $1" | |
name=`ps aux | grep --color=none $1 | grep --color=none -v grep` | |
waitpid $1 | |
t=`date "+%D @ %T"` | |
echo $name | mail -s "[Job Status] Process $1 on `hostname` has finished - $t" YOUREMAIL | |
else | |
t=`date "+%D @ %T"` | |
# If not reading from STDIN, send "nt" as message body, otherwise | |
# send the last 100 lines of output from previous command. | |
if test -t 0; then | |
echo "nt" | mail -s "[Job Status] Process on `hostname` has finished - $t" YOUREMAIL | |
else | |
readall | tail -n 100 | mail -s "[Job Status] Process on `hostname` has finished - $t" YOUREMAIL | |
fi | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment