linux自动重起服务,并发送邮件通知结果


点击(此处)折叠或打开

  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/user-attributes-database.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

上一篇:烂泥:通过binlog恢复mysql数据库


下一篇:消息称战略新兴板有望明年上半年推出