也没怎么学,看个大概,代码反正都是复制黏贴,凑合能用吧
各位大佬有 这方面的代码也发出来呀,我好学习学习
#!/bin/bash # # Func: Get Porcess Status In process.cfg # Define Variables HOME_DIR=”/root” CONFIG_FILE=”process.cfg” this_pid=$ function get_all_group { G_LIST=`sed -n ‘/\[GROUP_LIST\]/,/\[.*\]/p’ $HOME_DIR/$CONFIG_FILE | egrep -v “(^$|\[.*\])”` echo “$G_LIST” } function get_all_process { for g in `get_all_group` do P_LIST=`sed -n “/\[$g\]/,/\[.*\]/p” $HOME_DIR/$CONFIG_FILE | egrep -v “(^$|\[.*\])”` echo “$P_LIST” done } function get_process_pid_by_name { if [ $# -ne 1 ];then return 1 else pids=`ps -ef | grep $1 | grep -v grep | grep -v $0 | awk ‘{print $2}’` echo $pids fi } function get_process_info_by_pid { if [ `ps -ef | awk -v pid=$1 ‘$2==pid{print }’ | wc -l` -eq 1 ];then pro_status=”RUNNING” else pro_status=”STOPED” fi pro_cpu=`ps aux | awk -v pid=$1 ‘$2==pid{print $3}’` pro_mem=`ps aux | awk -v pid=$1 ‘$2==pid{print $4}’` pro_start_time=`ps -p $1 -o lstart | grep -v STARTED` } function is_group_in_config { for gn in `get_all_group`;do if [ $gn == $1 ];then return fi done echo “Group $1 is not in process.cfg” return 1 } function is_process_in_config { for pn in `get_all_process`;do if [ $pn == $1 ];then return fi done echo “Process $1 is not in process.cfg” return 1 } function get_all_process_by_group { is_group_in_config $1 if [ $? -eq 0 ];then p_list=`sed -n “/\[$1\]/,/\[.*\]/p” $HOME_DIR/$CONFIG_FILE | egrep -v “(^$|^#|\[.*\])”` echo $p_list else echo “GroupName $1 is not in process.cfg” fi } function get_group_by_process_name { for gn in `get_all_group`;do for pn in `get_all_process_by_group $gn`;do if [ $pn == $1 ];then echo “$gn” fi done done } function format_print { ps -ef | grep $1 | grep -v grep | grep -v $this_pid &> /dev/null if [ $? -eq 0 ];then pids=`get_process_pid_by_name $1` for pid in $pids;do get_process_info_by_pid $pid awk -v p_name=$1 \ -v g_name=$2 \ -v p_status=$pro_status \ -v p_pid=$pid \ -v p_cpu=$pro_cpu \ -v p_mem=$pro_mem \ -v p_start_time=”$pro_start_time” \ ‘BEGIN{printf “%-20s%-12s%-10s%-6s%-7s%-10s%-20s\n”,p_name,g_name,p_status,p_pid,p_cpu,p_mem,p_start_time}’ done else awk -v p_name=$1 -v g_name=$2 ‘BEGIN{printf “%-20s%-12s%-10s%-6s%-7s%-10s%-20s\n”,p_name,g_name,”STOPPED”,”NULL”,”NULL”,”NULL”,”NULL”}’ fi } function format_prints { awk ‘BEGIN{printf “%-20s%-10s%-10s%-6s%-7s%-10s%-20s\n”,”ProcessName———“,”GroupName—“,”Status—-“,”PID—“,”CPU—-“,”MEMORY—-“,”StartTime—“}’ for pn in `get_all_process`;do gn=`get_group_by_process_name $pn` is_process_in_config $pn && format_print $pn $gn done } function menu() { time=$(date “+%Y-%m-%d %H:%M:%S”) echo $time cat <>>菜单主页: `echo -e “\033[35m 1)sql导入导出\033[0m”` `echo -e “\033[35m 2)查看服务进程\033[0m”` `echo -e “\033[35m 3)主菜单\033[0m”` `echo -e “\033[35m Q)退出\033[0m”` EOF read -p “请输入对应序列号:” num1 case $num1 in 1) echo -e “\033[32m >>>sql导入导出-> \033[0m” sql_menu ;; 2) echo -e “\033[32m >>>服务进程-> \033[0m” format_prints ;; 3) echo -e “\033[32m >>>返回主菜单-> \033[0m” menu ;; Q|q) echo -e “\033[32m——–退出——— \033[0m” exit 0 ;; *) echo -e “\033[31m err:请输入正确的编号\033[0m” menu esac } function sql_menu() { cat<\033[0m” menu ;; *) echo -e “请输入正确编号” sql_menu esac } function sql_in() { cat<\033[0m” sql_menu ;; *) echo -e “导入数据库:${sql}” esac mysql -u root -proot ${sql} < ${sql}.sql sql_in } function sql_out() { cat<\033[0m” sql_menu ;; *) echo -e “导出数据库:${sql}” esac mysqldump -u root -proot ${sql} >${sql}.sql sql_out } menu复制代码
process.cfg文件 [GROUP_LIST] WEB DB HADOOP YUM JAVA [WEB] nginx httpd [DB] mysql oracle postgresql [HADOOP] datanode namenode journalnode [YUM] resourcemanager nodemanager [JAVA] java复制代码
广东省网友说:不错不错,支持一下
吉林省网友说:我也有一个,top
湖南省网友说:一天就可以搞出这样子了?你太厉害了!
河南省网友说:牛逼的 一天这水平 一个月直接去考红帽
海南省网友说:半年收购微软ibmapple