Welcome 微信登录

首页 / 操作系统 / Linux / Linux shell 检查进程PID

#
# check the pid of such program
#
checkPid() {
if [ -z "`ps x | grep $1 | grep -v grep | grep -v $0 | awk "{print $1}"`" ]; then
echo "The $1 program cant run well."
fi
}简单说明:ps 报告程序状况。ps x 显示所有程序,不以终端机来区分。$1 函数的第一个参数,如: checkPid test , 则: $1 = test,$0 = checkPid test。grep 查找文件里符合条件的字符串。ps x | grep $1 显示包含关键字"$1"的程序状况。ps x | grep $1 | grep -v grep | grep -v $0 去掉本身命令的影响awk "{print $1}" 将命令执行的显示中抽取数据包并格式化如:ps x | grep test | grep -v grep | grep -v "checkPid test"执行后显示为:12040 pts/1 Ss 0:00 test 那么, ps x | grep test | grep -v grep | grep -v "checkPid test" | awk "{print $1}"就显示为:12040即为该进程的PID。