一、根据PID过滤进程所有信息
#!/bin/bash
# Function: 根据用户输入的PID,过滤出该PID所有的信息
read -p "请输入要查询的PID:" PID
PROCESS_INFO=$(ps -aux | awk '$2 ~ /^'"$PID"'$/ { print $0 }')
if [ -z "$PROCESS_INFO" ]; then
echo "该PID不存在!!"
exit 1
fi
echo "--------------------------------"
echo "进程PID: $PID"
echo "进程命令:$(echo $PROCESS_INFO | awk '{print $11}')"
echo "进程所属用户: $(echo $PROCESS_INFO | awk '{print $1}')"
echo "CPU占用率:$(echo $PROCESS_INFO | awk '{print $3}')%"
echo "内存占用率:$(echo $PROCESS_INFO | awk '{print $4}')%"
echo "进程开始运行的时刻:$(echo $PROCESS_INFO | awk '{print $9}')"
echo "进程运行的时间:$(echo $PROCESS_INFO | awk '{print $10}')"
echo "进程状态:$(echo $PROCESS_INFO | awk '{print $8}')"
echo "进程虚拟内存:$(echo $PROCESS_INFO | awk '{print $5}')"
echo "进程共享内存:$(echo $PROCESS_INFO | awk '{print $6}')"
echo "--------------------------------"
二、根据进程名过滤进程信息会显示出该进程名包含的所有线程
#!/bin/bash
# Function: 根据输入的程序的名字过滤出所对应的PID,并显示出详细信息,如果有几个PID,则全部显示
read -p "请输入要查询的进程名:" NAME
PROCESSES=$(ps -aux | grep "$NAME" | grep -v grep)
if [ -z "$PROCESSES" ]; then
echo "该进程名没有运行!"
exit 1
fi
echo "--------------------------------"
i=1
while read -r PROCESS; do
echo "进程PID: $(echo $PROCESS | awk '{print $2}')"
echo "进程命令:$(echo $PROCESS | awk '{print $11}')"
echo "进程所属用户: $(echo $PROCESS | awk '{print $1}')"
echo "CPU占用率:$(echo $PROCESS | awk '{print $3}')%"
echo "内存占用率:$(echo $PROCESS | awk '{print $4}')%"
echo "进程开始运行的时刻:$(echo $PROCESS | awk '{print $9}')"
echo "进程运行的时间:$(echo $PROCESS | awk '{print $10}')"
echo "进程状态:$(echo $PROCESS | awk '{print $8}')"
echo "进程虚拟内存:$(echo $PROCESS | awk '{print $5}')"
echo "进程共享内存:$(echo $PROCESS | awk '{print $6}')"
echo "***************************************************************"
done <<< "$PROCESSES"
echo "--------------------------------"
三、根据用户名查询该用户的相关信息
优秀的 Shell 运维脚本
#!/bin/bash
# Function: 根据用户名查询该用户的相关信息
read -p "请输入要查询的用户名:" USERNAME
USER_INFO=$(ps -aux | grep "$USERNAME")
if [ -z "$USER_INFO" ]; then
echo "该用户名下没有运行的进程!"
exit 1
fi
echo "--------------------------------"
echo "用户名: $USERNAME"
echo "进程列表:"
echo "$USER_INFO"
echo "--------------------------------"
本文主题词: 常用shell运维脚本,运维shell脚本编程100例,运维shell脚本经典实例,运行shell脚本,跟老男孩学linux运维:shell编程实战,shell自动化运维,shell脚本运行环境,linux运维常用脚本,简述运行shell脚本的四种方法,shell脚本vi