一、shell 脚本
该脚本可以根据进程名和进程pid查询进程信息。
输出的进程信息包括:进程 PID、进程命令、进程所属用户、CPU占用率、内存占用率等
查询可选择模式:仅一次还是不限次。
#! /bin/bash
printProcessInfo(){
P=$1
echo "--------------------------------"
echo "进程PID: $1"
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 "--------------------------------"
}
searchByPName(){
read -p "请输入要查询的进程名:" NAME
N=`ps -aux | grep $NAME | grep -v grep | wc -l` ##统计进程总数
if [ $N -le 0 ];then
echo "该进程名没有运行!"
fi
i=1
echo "***************************************************************"
while [ $N -gt 0 ]
do
echo "进程PID: `ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $2}'`"
echo "进程命令:`ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $11}'`"
echo "进程所属用户: `ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $1}'`"
echo "进程开始运行的时刻:`ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $9}'`"
echo "进程运行的时间:` ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $11}'`"
echo "进程状态:`ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $8}'`"
echo "***************************************************************"
let N-- i++
done
}
searchByPid(){
echo "***"
echo "1. 仅一次"
echo "2. 无限次"
read -p "请选择查询选项: " c
n=`ps -aux| awk '$2~/^'$1'$/{print $11}'|wc -l`
if [ $n -eq 0 ];then
echo "该PID不存在!!"
exit
fi
if [ $c -eq 1 ]; then
printProcessInfo $1
elif [ $c -eq 2 ]; then
while true
do
printProcessInfo $1
sleep 2
done
else
echo "错误选项,请重新运行"
fi
}
echo "1. 进程名"
echo "2. 进程PID"
read -p "请输入查询的类型 :" s
if [ $s -eq 1 ]; then
searchByPName
fi
read -p "请输入进程 PID : " epid
searchByPid $epid
二、运行截图