Grep 和 pGrep 组成进程

Grep and pGrep making up processes

我根本不熟悉 bash,但我正在尝试制作一对脚本来检测

  1. 如果程序是运行。
  2. 如果 python/bash 脚本是 运行。

我的 nº1 代码是:

#!/bin/bash

X=$( pidof  )

if [ ${#X} -gt 0 ]
then
    echo " has already $X been started"
else
    echo " not started $X"
fi

效果很好,但无法检测到脚本,所以我做了第 2 项更改:

X=$( pgrep -f  )

起初 nº2 它似乎工作正常,但当我终止 python 脚本时,我仍然得到:

WebsocketServer has 5 length and it's already 11919 started

如果我这样做 ps -ax 进程的 PID 不会无处可见。

但是如果我写 ps -ax | grep websocket:

11921 pts/4    S+     0:00 grep --color=auto websocket

如果我启动 python 脚本...

WebsocketServer has 11 length and it's already 11927 11935 started

这是怎么回事?我是不是误用了命令?

编辑:忘记提到在终端 returns 中写入 pgrep -f WebsocketServer 没什么,就像它应该的那样。

问题是您的脚本的参数与您正在搜索的脚本名称相同,pgrep -f 正在查找脚本。

您可以尝试以下技巧:将名称分成两个参数。

checkScriptAlive websocket Server

然后在脚本中执行:

target=""
x=$(pgrep -f "$target")