脚本实现的结果:
[[email protected] ~]# sh /app/shell/app_status.sh
Processname---------Groupname-------Status-----PID----cpu----MEMORY----StarTime---
Nginx WEB StopED NulL NulL NulL NulL
httpd WEB StopED NulL NulL NulL NulL
MysqL DB RUNNING 3019 0.2 24.1 Wed Sep 11 12:02:21 2019
oracle DB StopED NulL NulL NulL NulL
[[email protected] ~]# sh /app/shell/app_status.sh Nginx
Processname---------Groupname-------Status-----PID----cpu----MEMORY----StarTime---
Nginx WEB RUNNING 4262 0.0 0.1 Wed Sep 11 12:18:19 2019
Nginx WEB RUNNING 4263 0.0 0.1 Wed Sep 11 12:18:19 2019
脚本使用的配置文件存放路径:/app/shell/process.cfg
process.cfg的内容
[GROUP_List]
WEB
DB
HADOOP
YUM[WEB]
Nginx
httpd[DB]
MysqL
oracle
postgresql[HADOOP]
datanode
namenode
journalnode[YUM]
resourcemanager
nodemanager
脚本内容:
#!/bin/bash
#
#Func:Get Porcess Status IN process.cfg
#define Variables
HOME_DIR="/app/shell/"
CONfig_file="process.cfg"
this_pID=$$
#
#get_all_goup:得到组的成员
#该函数无需输入参数,返回配置配置文件 process.cfg中所有的组信息,如WEB,DB等
#
function get_all_group
{
G_List=sed -n "/\[GROUP_List\]/,/\[.*\]/p" ${HOME_DIR}/${CONfig_file}|egrep -v "(^$|\[.*\])"
echo "${G_List}"
}
#get_all_process
#该函数无需输入任何参数,返回配置文件process.cfg中所有进程信息
#
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
}
#get_process_pID_by_name
#该函数接收一个参数,参数是进程名,返回的是一个进程PID的列表,可能是一个或是多个
#
function get_process_pID_by_name
{
if [ $# -ne 1 ];then
return 1
else
pIDs=ps -ef | grep | egrep -v grep|grep -v
ps -ef | awk -v pID= ‘==pID{print }‘ | wc -l
| awk ‘{print }‘
echo ${pIDs}
fi
}
#get_process_info_by_pID
#该函数是接收一个参数,参数进程PID;返回值是一进程运行信息的列表,其包含cpu 占用率 内存
#
function get_process_info_by_pID
{
if [ ps aux | awk -v pID= ‘==pID{print }‘
-eq 1 ];then
pro_status="RUNNING"
else
pro_status="StopED"
fi
pro_cpu=ps aux | awk -v pID= ‘==pID{print }‘
pro_mem=ps -p -o lstart | grep -v STARTED
pro_start_time=get_all_group
#echo "${pro_status} ${pro_cpu} ${pro_mem} "${pro_start_time}" "
}
#is_group_in_config
#该函数接收一个参数,参数为组的名称;返回是0或1;0代表该组在配置文件中,1代表不在
#
function is_group_in_config
{
for gn in get_all_process
;do
if [ "${gn}" == "$1" ];then
return
fi
done
echo "Group $1 is not in process.cfg"
return 1
}
#is_process_in_config
#该函数接收一个参数,参数为组名;返回值是对应组内所有进程名名称列表
#
function is_process_in_config
{
for pn in sed -n "/\[\]/,/\[.*\]/p" ${HOME_DIR}/${CONfig_file} | egrep -v "(^$|^#|\[.*\])"
;do
if [ $pn == $1 ];then
return
fi
done
}
#get_all_process_by_group
#该函数接收一个参数,参数为组的名称传给is_group_in_config返回的0列出该组内所有进程名
#
function get_all_process_by_group
{
is_group_in_config $1
if [ $? -eq 0 ];then
p_List=get_all_group
echo ${p_List}
else
echo "Groupname $1 is not in process.cfg"
fi
}
#get_group_by_process_name
#该函数接收一个参数,参数名是进程名;调用get_all_group得到组,get_all_process_by_group ${gn};判断是否在这个组内;若在到组名称
#
function get_group_by_process_name
{
for gn in get_all_process_by_group ${gn}
;do
for pn in get_process_pID_by_name
;do
if [ $pn == $1 ];then
echo "${gn}"
fi
done
done
}
#format_print
#该函数接收一个参数;参数是进程名,显示进程的Processname---------","Groupname-------","Status-----","PID----","cpu----","MEMORY----","StarTime---
#
function format_print
{
ps -ef | grep $1 | grep -v grep | grep -v $this_pID &> /dev/null
if [ $? -eq 0 ];then
pIDs=get_all_process_by_group $gn
for pID in $pIDs;do
get_process_info_by_pID $pID
awk -v name=$1 \
-v g_name=$2 \
-v status=$pro_status \
-v pID=$pID \
-v cpu=$pro_cpu \
-v mem=$pro_mem \
-v start_time="${pro_start_time}" \
‘BEGIN{printf "%-20s%-16s%-11s%-7s%-7s%-10s%-20s\n",name,g_name,status,pID,cpu,mem,start_time}‘
done
else
awk -v name=$1 -v g_name=$2 ‘BEGIN{printf "%-20s%-16s%-11s%-7s%-7s%-10s%-20s\n","StopED","NulL","NulL"}‘
fi
}
##
##
if [ ! -e ${HOME_DIR}/${CONfig_file} ];then
echo "${CONfig_file} is not exist..please check.."
exit 1
fi
##
##
awk ‘BEGIN{printf "%-20s%-16s%-10s%-6s%-7s%-10s%-20s\n","Processname---------","StarTime---"}‘
##
##
if [ $# -gt 0 ];then
if [ "$1" == "-g" ];then
shift
for gn in [email protected];do
is_group_in_config ${gn} || continue
for pn in get_group_by_process_name $pn
;do
is_process_in_config $pn && format_print $pn $gn
done
done
else
for pn in [email protected];do
gn=get_all_process
is_process_in_config $pn && format_print $pn $gn
done
fi
else
for pn in get_group_by_process_name $pn
;do
gn=
is_process_in_config $pn && format_print $pn $gn
done
fi
[[email protected] ~]#
以上是内存溢出为你收集整理的Linux shell 脚本 实现查询出进程的名字,cup内存占用率,启动时间在线状态等格式化输出全部内容,希望文章能够帮你解决Linux shell 脚本 实现查询出进程的名字,cup内存占用率,启动时间在线状态等格式化输出所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)