2020-03-17

今天再次完善之前我写的脚本(3月12号发布在此博客上),这个版本我定为2.0版本

之前那个是1.0版本,2.0版本相对于1.0版本增加了三个功能,安装服务中间件,开启服务器中间件,

卸载服务器中间件,增加的脚本代码如下





实验结果如下所示,用httpd服务为例

install service

open service


remove service



实验成功

代码如下所示

#!/bin/bash

# author :trippal

# time:2020/03/12/14:29

# in ShenZhen city ,GuangDong province,China

# read -p "please input your host IPaddress that you want to connect by ssh " p

# ssh $p

ifsucess=$?

while [ $ifsucess -eq 0 ]

do

  read -p " what do you want ? pidsearch ? checkcpu ? db-backup? ssh? zabbix-install-test? remove service? install service? test service? open service?  or just exit? " p

  case "$p" in

  "pidsearch")

  echo "pidsearch shell is running!"

  read -p "请输入要查询的PID: " P

  n=`ps -aux| awk '$2~/^'$P'$/{print $11}'|wc -l`

  if [ $n -eq 0 ];then

    echo "该PID不存在!!"

    continue

  fi

  echo "--------------------------------"

  echo "进程PID: $P"

  echo "进程命令:`ps -aux| awk '$2~/^'$P'$/{print $11}'`"

  echo "进程所属用户: `ps -aux| awk '$2~/^'$P'$/{print $1}'`"

  echo "CPU占用率:`ps -aux| awk '$2~/^'$P'$/{print $3}'`%"

  echo "内存占用率:`ps -aux| awk '$2~/^'$P'$/{print $4}'`%"

  echo "进程开始运行的时刻:`ps -aux| awk '$2~/^'$P'$/{print $9}'`"

  echo "进程运行的时间:`ps -aux| awk '$2~/^'$P'$/{print $10}'`"

  echo "进程状态:`ps -aux| awk '$2~/^'$P'$/{print $8}'`"

  echo "进程虚拟内存:`ps -aux| awk '$2~/^'$P'$/{print $5}'`"

  echo "进程共享内存:`ps -aux| awk '$2~/^'$P'$/{print $6}'`"

  echo "--------------------------------"

  ;;


  "checkcpu")

  echo -e "checkcpu shell is running!\n"

  echo "first of all, press the button of Ctrl and c can stop this script"

  echo -e "so,let us get the topic:\nnow the usage of cpu is"

  dstat | awk '{print ($1+$2)/100}'

  ;;

  "db-backup")


  echo "database-backup shell is running"


  ;;


  "exit")


  echo "the shell is over,goodbye"

  exit

  ;;

  "ssh")

  echo "now the service of ssh is running"

  read -p "please input your host IPaddress that you want to connect by ssh " p

  ssh $p

  ;;

  "install service")

  read -p "please input the name of service you want to install:" p

  ifsucess=$?

#  while [ $ifsucess == 0 ]

#  do

  echo "Input the ctrl and C button can exit the shell"

  yum install $p -y

#  ifsucess=$?

#  if [ ifsucess == 0 ]

#  then

#  break

#  fi



#  done

  echo "your service you want to install is sucessful installed!"

  ;;


  "remove service")

  read -p " please input the name of service you want to remove:" p

#  rpm -qa | grep $p

  ifsucess=$?

  echo $ifsucess

  while [ $ifsucess == 0 ]

  do

  yum remove $p -y

  break

  done

  echo -e " $p was removed!"

  ;;


  "open service")

  read -p "please input the name of service you want to open: " p

  if [ $? == 0 ]

  then

  service $p start

#  systemctl start $p.service

  systemctl status $p

  else

  echo "Input fail !please try again "

  fi

  ;;

  "zabbix-install-test")

  echo "now it is running the test of four requirement of zabbix-install"

  if_soft_install()

  {

  n=`rpm -qa|grep -cw "$1"`

    if [ $n -eq 0 ]

    then

    echo "$1 is not install."

    yum install -y $1

    else

    echo "$1 installed."

    echo "the version of $1 is"

    echo "-------------------"

    $1 -v

    echo "-------------------"

    fi

    }

    if_soft_install httpd

    if_soft_install nginx

    if_soft_install php

    if_soft_install mysql

    chk_service()

    {

    p_n=`ps -C "$1" --no-heading |wc -l`

    if [ $p_n -eq 0 ]

    then

    echo "$1 not start."

    /etc/init.d/$1 start

    else

    echo "$1 started."

    fi

    }

    chk_service httpd

    chk_service mysqld

    chk_service nginx

    chk_service php-fpm 

  ;;

  esac


done

echo -e "the connection to $p wasnt sucess,please check your network connection status\n"

echo "the running process of  shell script is over "

你可能感兴趣的:(2020-03-17)