2020-03-12

今天下午自己写了个临时的综合脚本,实现了几个功能:1根据PID查看该进程的相关信息;

2检查CPU的使用率;3 ssh连接某台主机;4 zabbix安装前的几个必要条件,即LNMP或LAMP的安装;5 数据库备份这个功能暂时没有实现;

trippal原创文章,请勿转载!






执行结果如下所示

1 pidsearch



如果该进程不存在,还是会继续跳过这次寻找


2 checkcpu


按ctrl+c可以退出该dstat命令


3 ssh连接



4 zabbix-install-test 这个因为我这台mysql服务器暂时不想要安装nginx和Apache,php这四个服务,所以该shell里的四个安装这些服务的函数暂时被我注释掉了,所以只有下面只有检测的结果

5 db-backup 这个功能暂时没法实现,估计冷备某个数据库也是需要些时间来写,所以这个地方暂时只有一条echo 打印语句


最后 想退出可以输入 exit 终止该shell脚本


最后贴上我写的脚本

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

  ;;

  "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."

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-12)