Shell / 脚本

大型脚本案例

# 需求描述:      
1、实现一个脚本工具,该脚提供类似supervisor功能,可以对进程进行管理     
2、一键查看所有进程运行状态     
3、单个或批量启动进程,单个或批量停止进程       
4、提供进程分组功能,可以按组查看进程运行状态,可以按组启动或停止组内所有进程  




# 配置文件process.cfg     
```
[GROUP_LIST]        
WEB     
DB      
HADOOP      
YARN        

[WEB]       
nginx       
httpd       

[DB]        
mysql       
postgresql      
oracle      

[HADOOP]        
datanode        
namenode        
journalnode 

[YARN]      
resourcemanager     
nodemanager     

[nginx]     
description="Web Server 1"      
program name=tail       
parameter=-f /root/shell/test.conf
```

# 脚本代码:
```
#!/bin/bash
#
#=============================================
#Author:            Sea Shi
#Chat:              351953026
#Date:              2020-03-11
#FileName:          app_status.sh
#URL:               http://www.alltek.com
#Description:       The Stduy of Script
#Copyright(c):      2020 All right reserved
#=============================================

HOME_DIR="/usr/local/checkserver"
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="STOPPED"
	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 -e "\033[37mGroupName $1 is not in process.cfg!\033[0m"
	exit 9  
}

function is_process_in_config
{
	for pn in `get_all_process`;do
		if [ $pn == $1 ];then
			return
		fi
	done
	echo -e "Process \033[1;5;37m$1 \033[0m is not in process.cfg!"
	return 2
}


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
		break 
	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`
		get_process_info_by_pid $pids
		for pid in $pids;do
			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 "%-16s%-10s%-10s%-8s%-6s%-10s%-15s\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 "%-16s%-10s%-10s%-8s%-6s%-10s%-15s\n",p_name,g_name,"STOPPED","NULL","NULL","NULL","NULL"}'
	fi
}

function title_format_print
{

	awk 'BEGIN{printf "%-16s%-10s%-10s%-8s%-6s%-10s%-15s\n","ProcessName-----","GroupName-","Status----","PID-----","CPU---","MEMORY----","StartTime------"}'
}


if [ ! -e $HOME_DIR/$CONFIG_FILE ];then
    echo "file  process.cfg is not exsit."
    exit 1
fi

title_format_print
if  [ $# -gt 0 ] ;then 
	if [ "$1" == "-g" ];then
		shift
		for gn in $@;do
			for pn in `get_all_process_by_group $gn`;do
				if [ $? -eq 0 ];then
					is_process_in_config $pn  &&format_print $pn $gn
				else
					echo -e "GroupName \033[1;5;37m$gn\033[0m is not in Process.cfg"
					break
						
				fi
			done
		done
	else
		for pn in $@ ; do
			gn=`get_group_by_process_name $pn`
			is_process_in_config $pn  && format_print $pn $gn
		done
	fi
else
	for pn in `get_all_process`;do
		gn=`get_group_by_process_name $pn`
		is_process_in_config $pn && format_print $pn $gn || echo -e "ProcessName \033[1;5;36m$pn\033[0m is not in Process.cfg!"
	done
fi
```

发表者

shi351953026@126.com
一个在IT苦海中遨游的老菜鸟

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注