Guest User

Untitled

a guest
Oct 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. readall () {
  2. read all
  3. while read line; do
  4. all="$all\n$line"
  5. done
  6.  
  7. echo -e $all
  8. }
  9.  
  10. waitpid () {
  11. pid=$1
  12. while true; do
  13. kill -0 $pid &> /dev/null
  14. if [ $? -ne 0 ]; then
  15. return
  16. fi
  17.  
  18. sleep 60
  19. done
  20. }
  21.  
  22. emailwhendone () {
  23. # There are four ways to run this:
  24. # $ emailwhendone PID
  25. # This traces a PID and emails you when it is no longer found in the list
  26. # of active processes.
  27. #
  28. # $ command && emailwhendone
  29. # Only send an email when command is successful.
  30. #
  31. # $ command ; emailwhendone
  32. # Send an email even if command fails
  33. #
  34. # $ command | emailwhendone
  35. # Send an email where the message body is the last 100 lines of output from
  36. # command.
  37. if [ $# -eq 1 ]; then
  38. echo "tracing pid $1"
  39. name=`ps aux | grep --color=none $1 | grep --color=none -v grep`
  40. waitpid $1
  41. t=`date "+%D @ %T"`
  42. echo $name | mail -s "[Job Status] Process $1 on `hostname` has finished - $t" YOUREMAIL
  43. else
  44. t=`date "+%D @ %T"`
  45. # If not reading from STDIN, send "nt" as message body, otherwise
  46. # send the last 100 lines of output from previous command.
  47. if test -t 0; then
  48. echo "nt" | mail -s "[Job Status] Process on `hostname` has finished - $t" YOUREMAIL
  49. else
  50. readall | tail -n 100 | mail -s "[Job Status] Process on `hostname` has finished - $t" YOUREMAIL
  51. fi
  52. fi
  53. }
Add Comment
Please, Sign In to add comment