实用shell脚本备份

lianshaohua 2014-05-21

#!/bin/bash
#chkconfig: 5 80 90
#description:check
#
 
# source function library
#. /etc/rc.d/init.d/functions

JAVA_HOME=/usr/bin
export JAVA_HOME=$JAVA_HOME
export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin

DIALUP_PID=/root/unixDialup/dialup.pid

CheckProcessStata()
{
    CPS_PID=$1
    if [ "$CPS_PID" != "" ] ;then
        CPS_PIDLIST=`ps -ef|grep $CPS_PID|grep -v grep|awk -F" " '{print $2}'`
    else
        CPS_PIDLIST=`ps -ef|grep "$CPS_PNAME"|grep -v grep|awk -F" " '{print $2}'`
    fi

    for CPS_i in `echo $CPS_PIDLIST`
    do
        if [ "$CPS_PID" = "" ] ;then
            CPS_i1="$CPS_PID"
        else
            CPS_i1="$CPS_i"
        fi

        if [ "$CPS_i1" = "$CPS_PID" ] ;then
            #kill -s 0 $CPS_i
            kill -0 $CPS_i >/dev/null 2>&1
            if [ $? != 0 ] ;then
                echo "[`date`] unixdialup-10500: Process $i have Dead"
                kill -9 $CPS_i >/dev/null 2>&1
              
                return 1
            else
                #echo "[`date`] unixdialup-10501: Process is alive"
                return 0
            fi
        fi
    done
    echo "[`date`] unixdialup-10502: Process $CPS_i is not exists"
    return 1
}


   echo "dialup  Service check ..."
   SPID=`cat /root/unixDialup/dialup.pid`
   CheckProcessStata $SPID >/dev/null
       if [ $? != 0 ];then
       nohup /root/bea/user_projects/domains/mydomain/startWebLogic.sh >nohup.out 2>&1 & new_agent_pid=$!
       #nohup ls >nohup.out 2>&1 & aaa=$!
       echo "$new_agent_pid" > $DIALUP_PID
       else
        echo "unixdialup:{$SPID} Running Normal."
       fi


 findchidenproc() {
  CPS_PID=$1
  tmpfile=/tmp/CheckProcessStat$$
  if [ "$CPS_PID" != "" ] ;then
      ps -ef|grep $CPS_PID|grep -v grep|awk -F" " '{print $2,$3}' > $tmpfile
    else
      echo "Warning:Input pid is null"
      return 1
  fi

  while read line
  do
    o_pid_1=`echo $line|awk '{print $1}'`
    o_pid_2=`echo $line|awk '{print $2}'`
        
    if [ "$CPS_PID" = "$o_pid_2" ];then
         echo "$o_pid_1"
         rm -f $tmpfile
         return 0
    fi
        
  done < $tmpfile
  rm -f $tmpfile
  return 1
}

findparentproc() {
  CPS_PID=$1
  tmpfile=/tmp/CheckProcessStat$$
  if [ "$CPS_PID" != "" ] ;then
      ps -ef|grep $CPS_PID|grep -v grep|awk -F" " '{print $2,$3}' > $tmpfile
    else
      echo "Warning:Input pid is null"
      return 1
  fi

  while read line
  do
    o_pid_1=`echo $line|awk '{print $1}'`
    o_pid_2=`echo $line|awk '{print $2}'`
        
    if [ "$CPS_PID" = "$o_pid_1" ];then
        if [ "$o_pid_2" = "1" ];then
            rm -f $tmpfile
            return 1
          else
             echo "$o_pid_2"
             rm -f $tmpfile
             return 0
        fi
    fi
        
  done < $tmpfile
  rm -f $tmpfile
  return 1
}

findallproc(){
  CPS_PID=$1
  tmpfile=/tmp/CheckProcessStat$$
  if [ "$CPS_PID" != "" ] ;then
      ps -ef|grep $CPS_PID|grep -v grep|awk -F" " '{print $2,$3}' > $tmpfile
    else
      echo "Warning:Input pid is null"
      return 1
  fi

  while read line
  do
    o_pid_1=`echo $line|awk '{print $1}'`
    o_pid_2=`echo $line|awk '{print $2}'`
   
    if [ "$CPS_PID"  = "$o_pid_1" ];then
         echo "$CPS_PID"
         if [ "$o_pid_2" = "1" ];then
               #echo "$o_pid_1 is Root process"
               chidrenopt=0
               chi_pid=$CPS_PID             
               while [ $chidrenopt = 0 ]
               do
                  chi_pid=`findchidenproc $chi_pid`
                  if [ $? != 0 ];then
                      chidrenopt=1
                    else
                      echo "$chi_pid"
                  fi
               done
            else
                parentopt=0
                chidrenopt=0
                par_pid=$CPS_PID
                chi_pid=$CPS_PID
                while [ $parentopt = 0 ]
                do
                   par_pid=`findparentproc $par_pid`
                   if [ $? != 0 ];then
                       parentopt=1
                     else
                       echo "$par_pid"
                   fi
                done
               
                while [ $chidrenopt = 0 ]
                do
                   chi_pid=`findchidenproc $chi_pid`
                   if [ $? != 0 ];then
                       chidrenopt=1
                     else
                       echo "$chi_pid"
                   fi
                done
              
         fi 
    fi
     
  done < $tmpfile
  rm -rf $tmpfile
  return 1
}

killproc () {
  pid=$1
  if [ "$pid" = "" ];then
       echo "Warning :input pid is null"
       return 1
     else
       proclist=`findallproc $pid`
       for i in $proclist
       do
          echo "kill -9 $i"
       done
   fi
}

 unixdialup开机自启动

1拷贝 check文件到/etc/rc.d/init.d/目录下面
 cp /root/unixdialup/check    /etc/rc.d/init.d/

2授予可执行权限
 chmod +x check
 
3使用chkconfig设置为系统的服务
 chkconfig --add check
 
4查看服务是否在列表内,查看是否有check
 chkconfig --list
 
5重启系统测试是check正常启动
 reboot
 
6启动后使用ps目录来查看java是否存在
 ps -ef|grep java
 
 
7 如果想卸载check服务
 chkconfig --del check



 

相关推荐