设为首页 加入收藏

TOP

Linux shell 检查进程PID
2014-11-24 00:45:21 来源: 作者: 【 】 浏览:4
Tags: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。


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇初识Linux脚本编程(shell) 下一篇Ubuntu 8.04下的嵌入式开发环境建..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: