文章目录
read -p "请输入要查询的进程名:" NAME
N = ` ps -aux | grep $NAME | grep -v grep | wc -l `
if [ $N -le 0 ] ; then
echo "该进程名没有运行!"
fi
i = 1
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 "CPU占用率:` ps -aux | grep $NAME | grep -v grep | awk 'NR==' $i'{print $0}' | awk '{print $3}' ` %"
echo "内存占用率:` ps -aux | grep $NAME | grep -v grep | awk 'NR==' $i'{print $0}' | awk '{print $4}' ` %"
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 "进程虚拟内存:` ps -aux | grep $NAME | grep -v grep | awk 'NR==' $i'{print $0}' | awk '{print $5}' ` "
echo "进程共享内存:` ps -aux | grep $NAME | grep -v grep | awk 'NR==' $i'{print $0}' | awk '{print $6}' ` "
echo "***************************************************************"
let N-- i++
done
#!/bin/bash
read -p "请输入要过滤的进程名关键字: " process_name
pids = $( pgrep -f "$process_name " )
if [ -n "$pids " ] ; then
echo "找到以下匹配的进程信息:"
for pid in $pids ; do
echo "进程ID: $pid "
echo "进程信息:"
ps -p "$pid " -o pid,ppid,pgid,sid,user,%cpu,%mem,vsz,rss,tty,stat,start,time,cmd
echo "------------------------------------"
done
else
echo "未找到进程名包含关键字 '$process_name ' 的进程"
fi