shell服务管理->

nginx.php等服务管理练习脚本

 

->nginx的启动状态

root@jumpserver-70 day02]# cat nginx_web.sh 
#!/bin/bash
source /etc/init.d/functions

nginx_status() {
    if [ $? -eq 0 ];then
        action "nginx is $1 OK!" /bin/true
    else
        action "nginx is $1 failed!" /bin/false
    fi
}

case $1 in 
    start)
        nginx &>/dev/null
        nginx_status $1
        ;;
    stop)
        nginx -s stop &>/dev/null
        nginx_status $1
        ;;
    reload)
        nginx -s reload &>/dev/null
        nginx_status $1
        ;;
    *)
        echo "USAGE:$0{start|stop|reload}"
esac

 

 

->安装nginx->查看状态

echo "===============================System Repo============================="
Repos=$(yum repolist |grep nginx|wc -l)
if [ $Repos -eq 0 ];then

    echo "没有发现Nginx的yum仓库...尝试中"

    cat >/etc/yum.repos.d/nginx.repo <<-EOF                         #使用cat方法导入
    [nginx]
    name=nginx repo
    baseurl=http://nginx.org/packages/centos/7/x86_64/
    gpgcheck=0
    enabled=1
    EOF

    yum makecache &>/dev/null                                      #清空缓存

    Repos2=$(yum repolist |grep nginx|wc -l)            #再次yum尝试安装

    if [ $Repos2 -eq 1 ];then
        echo "yum仓库已经安装完成...."
    else
        echo "请检查你的网络环境...."
        exit 1
    fi
else

    echo "已存在Nginx的Repos仓库文件..."

fi

echo "===============================System Nginx Install======================="

Rpm_Nginx=$(rpm -qa nginx | wc -l)

if [ $Rpm_Nginx -gt 0 ];then
    echo "Nginx 已经安装....."
else
    echo "尝试安装Nginx...."
    yum install nginx -y &>/dev/null    
fi

echo "=======================System Nginx Status ======================"

Nginx_Status=$(systemctl status nginx|grep running|wc -l)
if [ $Nginx_Status -eq 1 ];then

    echo "Nginx已经启动"

else
    echo "Nginx尝试启动"

    pkill httpd &>/dev/null        
    pkill nginx &>/dev/null

    systemctl start nginx &>/dev/null

    if [ $? -eq 0 ];then
        Nginx_Status2=$(systemctl status nginx|grep Active|awk '{print $2 $3}')
        echo "nginx启动完成, 当前的状态是: $Nginx_Status2"
    else
        echo "启动失败, 请手动排查......"
    fi
fi

 

 

->nginx状态管理

[root@jumpserver-70 scripts]# cat nginx_status.sh 
#!/bin/bash

echo "-----------------------"
echo "1:开启"
echo "2:停止"
echo "3:重载"
echo "4:重启"
echo "5:状态"
echo "-----------------------"


read -p "请输入您要执行的命令:" command

case $command in 
    1)
    status_start=$(systemctl status nginx | egrep "running" |wc -l)
    if [ $status_start -eq 1 ];then
        
        echo "nginx已经启动,不需要执行启动操作"
    else
        systemctl start nginx &>/dev/null
        echo "nginx已经启动"
    fi    
        ;;
    2)
        systemctl stop nginx &>/dev/null
        echo "nginx已经停止"
        ;;
    3)
    status_start=$(systemctl status nginx | egrep "running" |wc -l)
        if [ $status_start -eq 1 ];then
        systemctl reload nginx &>/dev/null
        echo "nginx已经重载"
    fi
        ;;
    4)    
        systemctl restart nginx &>/dev/null
        echo "nginx已经重启"
        ;;
    5)
        systemctl status nginx 
        ;;
    *)
        echo "请您选择正确的命令"
        exit 1
esac    

 

 

->nginx状态监控脚本(zabbix)

[root@web03 day01]# cat nginx_status.sh 
#!/bin/bash
############################################################
# $Name:         nginx_status.sh
# $Version:      v1.0
# $Function:     Nginx Status
# $Author:       
# $organization: 
# $Create Date:  2018-9-19
# $Description:  Monitor Nginx Service Status
############################################################

NGINX_COMMAND=$1
NGINX_PORT=80

nginx_active(){
    /usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk '/Active/ {print $NF}'
}

nginx_reading(){
    /usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk '/Reading/ {print $2}'
}

nginx_writing(){
    /usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk '/Writing/ {print $4}'
       }

nginx_waiting(){
    /usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk '/Waiting/ {print $6}'
       }

nginx_accepts(){
    /usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk 'NR==3 {print $1}'
       }

nginx_handled(){
    /usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk 'NR==3 {print $2}'
       }

nginx_requests(){
    /usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk 'NR==3 {print $3}'
       }


  case $NGINX_COMMAND in
    active)
        nginx_active;
        ;;
    reading)
        nginx_reading;
        ;;
    writing)
        nginx_writing;
        ;;
    waiting)
        nginx_waiting;
        ;;
    accepts)
        nginx_accepts;
        ;;
    handled)
        nginx_handled;
        ;;
    requests)
        nginx_requests;
        ;;
          *)
        echo $"USAGE:$0 {active|reading|writing|waiting|accepts|handled|requests}"
    esac

->简单参考版 [root@web03 scripts]# cat nginx_status.sh #!/bin/bash port=80 while true do if [ $# -eq 1 ];then case $1 in active) curl -s 127.0.0.1${port}/status | awk '/Active/{print $NF}' break ;; accepts) curl -s 127.0.0.1${port}/status | awk 'NR==3{print $1}' break ;; handled) curl -s 127.0.0.1${port}/status | awk 'NR==3{print $2}' break ;; request) curl -s 127.0.0.1${port}/status | awk 'NR==3{print $3}' break ;; *) echo "USAGE $0 {active|accepts|requests|handled}" exit esac else echo "参数只能为1个,请参考{active|accepts|requests|handled}" break fi done

 

 

->根据系统版本安装yum源

[root@jumpserver-70 scripts]# cat repo.sh 
#!/bin/bash

os_name=$(cat /etc/redhat-release )
os_version=$(cat /etc/redhat-release |awk -F " " '{print $4}'| awk -F "." '{print $1}')

if [ $os_version -eq 7 ];then

    echo "这是$os_name的系统,请安装centos7的yum源"
    wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo &>/dev/null
    echo "$os_name:yum源安装完成"

elif [ $os_version -eq 6 ];then

    echo "这是$os_name系统,请安装centos6的yum源"
    wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo &>/dev/null
    echo "$os_name:yum源安装完成"

elif [ $os_version -eq 5 ];then 
    
    echo "这是$os_name系统,请安装centos6的yum源"
    wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo  &>/dev/null
    echo "$os_name:yum源安装完成"

else
    echo "请检查系统的版本"

fi

 

 

->选择php版本安装

[root@jumpserver-70 scripts]# cat php.sh 
#!/bin/bash

echo '1=(php-5.0)'
echo '2=(php-6.0)'
echo '3=(php-7.0)'

read -p "请输入要安装的版本:" num

if [ $num -eq 1 ];then

    echo "正在安装php5.0"

elif [ $num -eq 2 ];then

    echo "正在安装php6.0"

elif [ $num -eq 3 ];then 
    
    echo "正在安装7.0"
else
    echo "请选择安装版本"
fi

 

posted @ 2018-09-25 11:23  少校的小木屋  阅读(299)  评论(0编辑  收藏  举报