今天下午自己寫了個臨時的綜合腳本,實現(xiàn)了幾個功能:1根據(jù)PID查看該進程的相關(guān)信息;
2檢查CPU的使用率;3 ssh連接某臺主機;4 zabbix安裝前的幾個必要條件,即LNMP或LAMP的安裝;5 數(shù)據(jù)庫備份這個功能暫時沒有實現(xiàn);
trippal原創(chuàng)文章,請勿轉(zhuǎn)載!
執(zhí)行結(jié)果如下所示
1 pidsearch
如果該進程不存在,還是會繼續(xù)跳過這次尋找
2 checkcpu
按ctrl+c可以退出該dstat命令
3 ssh連接
4 zabbix-install-test 這個因為我這臺mysql服務(wù)器暫時不想要安裝nginx和Apache,php這四個服務(wù),所以該shell里的四個安裝這些服務(wù)的函數(shù)暫時被我注釋掉了,所以只有下面只有檢測的結(jié)果
5 db-backup 這個功能暫時沒法實現(xiàn),估計冷備某個數(shù)據(jù)庫也是需要些時間來寫,所以這個地方暫時只有一條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 "內(nèi)存占用率:`ps -aux| awk '$2~/^'$P'$/{print $4}'`%"
? echo "進程開始運行的時刻:`ps -aux| awk '$2~/^'$P'$/{print $9}'`"
? echo "進程運行的時間:`ps -aux| awk '$2~/^'$P'$/{print $10}'`"
? echo "進程狀態(tài):`ps -aux| awk '$2~/^'$P'$/{print $8}'`"
? echo "進程虛擬內(nèi)存:`ps -aux| awk '$2~/^'$P'$/{print $5}'`"
? echo "進程共享內(nèi)存:`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 "
給個打賞,謝謝!