shell 實戰 -- 基於一個服務啟動,關閉,狀態檢查的腳本


功能說明:

  • check:檢查服務狀態,在開啟,關閉,狀態檢查時都會用到這個函數,所以封裝起來放到最前面
  • start:開啟服務
  • stop:關閉服務
  • fstop:強制關閉
  • status:檢查服務狀態
  • running:查看所有正在運行的狀態

 

使用說明:

開啟服務:

啟動一個進程監聽 9000(默認)端口:

  • sh standard_server.sh start

 

啟動一個進程監聽 8000(自定義)端口:

  • 格式: sh standard_server.sh start ipaddress port
  • sh standard_server.sh start 10.10.4.200 8000


關閉服務:

關閉監聽 9000(默認)端口:

  • sh standard_server.sh stop

 

關閉監聽 8000(自定義)端口:

  • 格式: sh standard_server.sh stop ipaddress port
  • sh standard_server.sh stop 10.10.4.200 8000


查看監聽狀態:

查看 9000 端口(默認)監聽狀態:

  • sh standard_server.sh status

 

查看 8000端口(自定義)監聽狀態:

  • 格式: sh standard_server.sh status ipaddress port
  • sh standard_server.sh status 10.10.4.200 8000

 

查看所有正在監聽端口:

  • sh standard_server.sh running

 

代碼部分:

#!/bin/bash
# author: ck

check()
{
    if (($(ps aux|grep manage.py| grep "${port:-9000}" |grep -v grep|wc -l) == 0));then
        # stopped
        return 1;
    else
        # running
        return 0;
    fi
}

start()
{
    check
    if (($? == 1));then
        echo -n "standard server ${port:-9000} to start......"
        path=$(dirname $0)
        if [[ $path != '.' ]];then
            cd $path
        fi
        # nohup /home/seemmo/share/python/centos/python3.6.6/bin/python3 manage.py start_server -h "${host:-0.0.0.0}" -p "${port:-9000}" -k wf >nohup.out 2>&1 &
        nohup python3 manage.py start_server -h "${host:-0.0.0.0}" -p "${port:-9000}" -k wf >nohup.out 2>&1 &
        while true
        do
            check
            if (($? == 1));then
                echo -n '...'
                sleep 1
            else
                echo -e '\033[32mstarted\033[1m\033[0m'
                break
            fi
        done
    else
        echo "standard server ${port:-9000} has been running!!!"
    fi
}

fstop()
{
    check
    if (($? == 1));then
        echo "standard server ${port:-9000} has been stopped!!!"
    else
        echo -n "standard server ${port:-9000} force to stop....."
        ps aux|grep manage.py |grep "${port:-9000}" |grep -v grep|awk '{print $2}'|xargs kill -9
        while true
        do
            check
            if (($? == 1));then
                echo -e '\033[32mstopped\033[1m\033[0m'
                break
            else
                echo -n '...'
                sleep 1
            fi
        done
    fi
}

stop()
{
    check
    if (($? == 1));then
        echo "standard server ${port:-9000} has been stopped!!!"
    else
        echo -n "standard server ${port:-9000} to stop....."
        spid=1
        tp_list=($(ps aux|grep manage.py |grep "${port:-9000}" |grep -v grep|awk '{print $2}'|xargs))
        for tpid in ${tp_list[@]}
        do
            if ((spid == 1));then
                spid=$tpid
            elif ((tpid < spid));then
                spid=$tpid
            fi
        done
        kill -15 $spid
        retry_time=3
        while true
        do
            if ((retry_time == 0));then
                echo
                fstop
                break
            fi
            check
            if (($? == 1));then
                echo -e '\033[32mstopped\033[1m\033[0m'
                break
            else
                ((retry_time=retry_time-1))
                echo -n '.'
                sleep 1
            fi
        done
    fi
}

status()
{
    check
    if (($? == 1));then
        echo -e "standard server ${port:-9000} now is \033[32mstopped\033[1m\033[0m"
    else
        echo -e "standard server ${port:-9000} now is \033[32mrunning\033[1m\033[0m"
    fi
}

running()
{
    port_list=$(ps aux | grep "manage.py" | grep -v "grep" |awk '{print $17}' |xargs)
    for port in ${port_list}
    do
        echo -e "standard server ${port} now is \033[32mrunning\033[1m\033[0m"
    done
}

restart() {
    check
    if (($? == 1));then
        start
    else
        stop
        while true
        do
            check
            if (($? == 1));then
                start
                break
            else
                sleep 1
            fi
        done
    fi
}

if (($# == 1)) || (($# == 3));then
    if (($# == 3));then
        host=$2
        port=$3
    fi

    case $1 in
        start|stop|status|restart|fstop|running)
            $1
            ;;
        *)
            if (($# == 1));then
                echo "Usage: bash $0 {start|stop|status|restart|fstop|running}"
                exit 2
            else
                echo "Usage: bash $0 {start|stop|status|restart|fstop|running} host port"
                exit 2
            fi
    esac

else
    echo "Usage: bash $0 {start|stop|status|restart|fstop|running} host port"
    exit 2
fi

 

 

 

ending ~

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM