linux自動重起服務,併傳送郵件通知結果

天飛.h發表於2012-05-14

點選(此處)摺疊或開啟

  1. #! /bin/bash
  2. PATH=/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/app/service/bin:/app/service/bin:/usr/local/java/jdk1.6.0_20/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/app/service/bin
  3. DATETIME=`date +%F`
  4. serpath=/app/service/ser/bin
  5. serlog=/app/service/serrestart.log
  6. serdate=`date +%F.%T`
  7. serip=`/sbin/ifconfig |grep `inet addr`|grep 192 |awk F: `{print $2}`|awk `{print $1}``
  8. seruser=`whoami`
  9. #####mailsend
  10. mailtitle=“ser information $serdate $serip “
  11. mailmessagefile=$HOME/mail.txt #郵件資訊內容
  12. mailmessage= #郵件資訊內容
  13. #mailcatcher=noc@test.com
  14. mailcatcher=talen.hao@test.com #郵件接收者
  15. mailsender=ser@test.com #郵件傳送者
  16. serPIDFILE=/app/service/ser/userattributesdatabase.pid
  17. #####
  18. statusser()
  19. {
  20.         ps ef |grep ser |grep java|grep v grep |awk `{print $2}`
  21. }
  22. Uadrestart()
  23. {
  24.         cd $serpath
  25.         echo “>>>>> $serdate : 當前目錄 `pwd`”
  26.         pidold=`statusser`
  27.         echo “>>>>> $serdate : Old PID NO. is “
  28.         #./misctrl.sh stop
  29.         kill 9 $pidold
  30.         rm v $serPIDFILE
  31.         sleep 1
  32.         ./misctrl.sh start
  33.         pidnew=`statusser`
  34.         echo “>>>>> $serdate : New PID NO. is “
  35. }
  36. Status(){
  37.         if [ f $serPIDFILE ]
  38.         then
  39.                 if test z $pidnew o $pidnew = $pidold
  40.                 then
  41.                         mailmessage=`ser service restarted failure`
  42.                         echo “$serdate : $serip $mailmessage” > $mailmessagefile
  43.                         echo “$serdate : Old PID NO. is “ >> $mailmessagefile
  44.                         echo “$serdate : New PID NO. is “ >> $mailmessagefile
  45.                         echo “$serdate : restarted faild ==> $mailmessage”
  46.                         isok=failure
  47.                 else
  48.                         mailmessage=`ser service restarted success`
  49.                         echo “$serdate : $serip $mailmessage” > $mailmessagefile
  50.                         echo “$serdate : Old PID NO. is “ >> $mailmessagefile
  51.                         echo “$serdate : New PID NO. is “ >> $mailmessagefile
  52.                         echo “$serdate : restarted ok ==> $mailmessage”
  53.                         isok=success
  54. fi
  55.         else
  56.                 mailmessage=`ser service restarted failure`
  57.                 echo “$serdate : $serip $mailmessage” > $mailmessagefile
  58.                 echo “$serdate : Old PID NO. is “ >> $mailmessagefile
  59.                 echo “$serdate : New PID NO. is “ >> $mailmessagefile
  60.                 echo “$serdate : restarted faild ==> $mailmessage”
  61.                 isok=failure
  62.                 exit 1
  63. fi
  64. }
  65. MailSend()
  66. {
  67.         `which mail` v s “$mailtitle $isok” $mailcatcher f $mailsender $mailmessagefile
  68. }
  69. vim.mailrc()
  70. {
  71.         cp v $HOME/.mailrc{,.bak$DATETIME`echo $RANDOM`}
  72.         echo `set from=sender@126.com smtp=smtp.126.com` > $HOME/.mailrc
  73.         echo `set smtp-auth-user=sender@126.com smtp-auth-password=sender.com` >> $HOME/.mailrc
  74.         echo `set smtp-auth=login` >> $HOME/.mailrc
  75. }
  76. MAIL()
  77. {
  78.         if test f $HOME/.mailrc
  79.         then
  80.                 MailSend
  81.         else
  82.                 echo “.mailrc檔案不存在,將建立此檔案”
  83.                 vim.mailrc
  84.                 MailSend
  85.         fi
  86. }
  87. if [ $seruser == service ]
  88. then
  89.         echo “>>>>>$serdate : 開始執行重起服務”
  90.         Uadrestart
  91.         Status
  92.         MAIL
  93.         exit 0
  94. else
  95.         echo “>>>>>$serdate : 嚴重!!使用service 使用者執行此指令碼”
  96.         exit 1
  97. fi


相關文章