Advertisement
Guest User

Untitled

a guest
May 20th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # 如果需要监控指定命令的执行结果,失败时自动发邮件,就只需在最前面用此命令执行即可,例如 monitor echo "hello world"
  4.  
  5. # 得到当前脚本文件所在的绝对路径
  6. basePath=$(cd `dirname $0`; pwd)
  7.  
  8. # 执行并记录状态码
  9. # 如果原本参数里有双引号或单引号的话,直接执行 '$@',会失败
  10. # 用 echo "$@" 打印后看不到原本的双引号或单引号,例如 hive -e "select * from test" 就会变成 hive -e select * from test
  11. # 使用 exec 可正常执行,但是 exec 后面的代码就不会再执行了,所以需要将 exec 包装一下,原本的退出码也会正常返回
  12. ${basePath}/monitor_exec_wrapper "$@"
  13. result=$?
  14. echo "Result: ${result}"
  15.  
  16. # 恢复原本参数中的引号,单引号会被恢复成双引号,因此此结果只能做参考
  17. originParams=""
  18. whitespace="[[:space:]]"
  19. blank_str=" "
  20. for i in "$@"
  21. do
  22. if [[ $i =~ $whitespace ]]; then
  23. i="\"$i\""
  24. fi
  25. originParams=${originParams}${blank_str}${i}
  26. done
  27. echo "originParams: ${originParams}"
  28.  
  29. # 执行失败发邮件
  30. if [[ ${result} -ne 0 ]]; then
  31. echo "Shell script execute failed. Script: ${originParams}. result: ${result}"
  32. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement