Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- }
Add Comment
Please, Sign In to add comment