shell combat - based on a service startup, shutdown, check the status of the script

Function Description:

  • check: Check the service status, in the open, closed, will be used to check the state of the function, so up into the top of the package
  • start: open service
  • stop: turn off service
  • fstop: forced to close
  • status: check the service status
  • running: View the status of all running

 

Instructions for use:

Start the service:

Start a process monitor 9000 (default) Port:

  • sh standard_server.sh start

 

Start a process monitor 8000 (Custom) Port:

  • Format: sh standard_server.sh start ipaddress port
  • sh standard_server.sh start 10.10.4.200 8000


Close Services:

Close listening 9000 (default) Port:

  • sh standard_server.sh stop

 

Close listening 8000 (Custom) Port:

  • Format: sh standard_server.sh stop ipaddress port
  • sh standard_server.sh stop 10.10.4.200 8000


View monitor status:

View 9000 port (default) listening state:

  • sh standard_server.sh status

 

View port 8000 (Custom) listening state:

  • Format: sh standard_server.sh status ipaddress port
  • sh standard_server.sh status 10.10.4.200 8000

 

View all listening ports:

  • sh standard_server.sh running

 

Code section:

#!/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 ~

 

Guess you like

Origin www.cnblogs.com/kaichenkai/p/11224152.html