Skip to content

Instantly share code, notes, and snippets.

Created June 19, 2012 18:37
Show Gist options
  • Save anonymous/2955793 to your computer and use it in GitHub Desktop.
Save anonymous/2955793 to your computer and use it in GitHub Desktop.
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