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

相关文章:

  • 2022-12-23
  • 2021-12-19
  • 2022-01-30
  • 2022-01-21
  • 2022-12-23
  • 2021-12-11
  • 2021-12-27
  • 2022-12-23
猜你喜欢
  • 2022-03-09
  • 2021-07-15
  • 2022-02-13
  • 2022-12-23
  • 2021-12-26
  • 2022-12-23
  • 2021-09-05
相关资源
相似解决方案