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

功能說明:

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

 

使用說明:

開啓服務:python

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

  • sh standard_server.sh start

 

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

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


關閉服務:函數

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

  • sh standard_server.sh stop

 

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

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


查看監聽狀態:code

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

  • sh standard_server.sh status

 

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

  • 格式: 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 ~

相關文章
相關標籤/搜索