autossh 啓動腳本

#!/bin/bash
#
#chkconfig: 2345 80 90
#
# description:  Starts, stops and restart autossh service
#
# autossh startup Script

export autossh=/usr/local/autossh/bin/autossh
export AUTOSSH_LOGFILE=/usr/local/autossh/log/autossh.log
export AUTOSSH_PIDFILE=/var/run/autossh.pid
export AUTOSSH_POLL=180
export AUTOSSH_FIRST_POLL=30
export AUTOSSH_GATETIME=0
export AUTOSSH_DEBUG=1

user="root" 
R_host="172.16.100.208"



start() {
     # Do not start if there is no autossh.

     if [ ! -x "$autossh" ];then
         echo " Fail : $autossh not found."

     else

        if [ -f "$AUTOSSH_PIDFILE" ]; then
           echo "  >>> autosh is already running ... "
        fi

        # 9998 監視端口    10086-在208上起的端口  22-綁定的本地端口
        $autossh -M 9998 -NfR 10086:localhost:22 root@$R_host

        if [ $? -eq 0 ]; then
           echo "  >>> Start autossh sucesses..."
        fi 
     fi
}

stop() {
     #Do not stop if not found $AUTOSSH_PIDFILE

     if [  -f  "$AUTOSSH_PIDFILE" ]; then

        export pid=$(cat "$AUTOSSH_PIDFILE")
        num=$(ps -ef|awk  '{if($2~/'$pid'/) print $2}'|wc -l)

       if [[ $num -gt 0  ]]; then

          ps -ef|awk  '{if($2~/'$pid'/) print $2}'|xargs kill
          rm -f "$AUTOSSH_PIDFILE"
          echo "  >>> Autossh stop sucesses..."
       fi

     else

        echo "  >>> Autossh not running , exit .."

     fi

}

status() {

     if [  -f  "$AUTOSSH_PIDFILE" ]; then
        export pid=$(cat $AUTOSSH_PIDFILE)
        echo "  >>> autossh-daemon (pid "$pid") is running..."

     else
        echo "  >>> Autossh is is stopped. "
     fi   
}
     
restart() {

        stop;
        start
}

case "$1" in 
   start)
   start
   ;;
   stop)
   stop
   ;;
   status)
   status
   ;;
   restart)
   restart
   ;;
   *)
   echo $"Usage: $0 {start|stop|status|restart}"
   exit 1
   ;;
esac


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章