linux shell 检查进程PID

    技术2022-05-20  46

    转自:http://www.linuxdiyf.com/viewarticle.php?id=108103

     

    # # 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。


    最新回复(0)