用于标识正在运行的进程的Bash脚本?

用于标识正在运行的进程的Bash脚本?,bash,shell,Bash,Shell,我已经编写了一个bash脚本来识别一个进程,并相应地响应该进程是否正在运行。我的剧本: #!/bin/bash var1=$(ps -ef | grep -v grep | grep crond) echo $var1 if [$var1] then echo "The Process is Running" else echo "The process is not running" fi 我正在从Var1变量中获取输出,但代码仍然在“if条件”处中断,并出现以下错误: $ ./test.

我已经编写了一个bash脚本来识别一个进程,并相应地响应该进程是否正在运行。我的剧本:

#!/bin/bash

var1=$(ps -ef | grep -v grep | grep crond)
echo $var1
if [$var1]
then
echo "The Process is Running"
else
echo "The process is not running"
fi
我正在从Var1变量中获取输出,但代码仍然在“if条件”处中断,并出现以下错误:

$ ./test.sh
spidmd07 3034 4649 0 2017 ? 00:00:00 crond root 4649 1 0 2017 ? 00:00:02 crond spidmd01 5477 4649 0 2017 ? 00:00:00 crond
./test.sh: line 6: [spidmd07: command not found
The process is not running

在本例中,spidmd07是另一个正在运行1个“Crond”进程实例的用户。

请尝试以下操作,如果这对您有帮助,请告诉我。其中,我将
[$var]
更改为
[[[-n“$var”]]
中的
if
条件

#!/bin/bash

var1=$(ps -ef | grep -v grep | grep crond)
echo "$var1"
if [[ -n "$var1" ]]
then
    echo "The Process is Running"
else
    echo "The process is not running"
fi

尝试以下内容,并让我知道这是否有助于你。其中,我将
[$var]
更改为
[[[-n“$var”]]
中的
if
条件

#!/bin/bash

var1=$(ps -ef | grep -v grep | grep crond)
echo "$var1"
if [[ -n "$var1" ]]
then
    echo "The Process is Running"
else
    echo "The process is not running"
fi

对一个过程进行grep总是很麻烦的。有点像海森堡测不准原理;你的grep过程会显示你想要的结果

如果你想使用grep,你可以调整模式,这样grep进程就不会出现像
grep[c]ron
这样的东西。或者更好,使用ps-C选项让ps按命令名选择,然后跳过grep:

if [ -n "$(ps -C crond -o pid=)" ]; then
    # if the ps command returns any string ...
    echo "The Process is Running"
else
    echo "The process is not running"
fi

使用-o选项可以选择打印的内容。在这种情况下,我们只需要进程id。

对进程进行grep-ps总是很麻烦的。有点像海森堡测不准原理;你的grep过程会显示你想要的结果

如果你想使用grep,你可以调整模式,这样grep进程就不会出现像
grep[c]ron
这样的东西。或者更好,使用ps-C选项让ps按命令名选择,然后跳过grep:

if [ -n "$(ps -C crond -o pid=)" ]; then
    # if the ps command returns any string ...
    echo "The Process is Running"
else
    echo "The process is not running"
fi

使用-o选项可以选择打印的内容。在本例中,我们只需要进程id。

是的,如果括号中的语法错误,您就可以找到。您可以使用
[
[[
,但两者后面都需要一个空格(有关经典测试程序的更多信息,请参阅
man[

但是,我假设您希望它不仅仅适用于
crond
,所以我将它制作成一个带有arg的脚本

$ cat is_cmd_running.sh 
#!/bin/bash

psout=$(ps -ef | grep -v grep | grep -v $0 | grep $1) 
if [[ -z ${psout:-} ]]; then
  echo "$1 is not running"
else
  echo "$1 is running"
fi
请注意,除了过滤器
-v grep
,我还必须过滤
-v$0
,否则它会自动匹配程序的ps。此程序在我的系统上工作:

$ bash is_cmd_running.sh firefox
firefox is running
$ bash is_cmd_running.sh konqueror
 konqueror is not running
还有一个更简单的版本,您不需要测试变量,只需计算上一个
grep
的返回代码。通常的做法是使用
-q
标志使grep安静,然后只计算返回代码。我认为,在shell脚本中,这是更传统的做法:

$ cat is_cmd_running.sh 
#!/bin/bash

if ps -ef | grep -v grep | grep -v $0 | grep -q $1 ; then
  echo "$1 is running"
else
  echo "$1 is not running"
fi
这也适用于:

$ bash is_cmd_running.sh konqueror
konqueror is not running
$ bash is_cmd_running.sh firefox
firefox is running

编辑:

@tripleee很好地说明了标准命令的重新实现和linter的重要性(谢天谢地,我安装了
shellcheck
)因此,我提供了一个最终版本,该版本被
shellcheck
接受,它演示了如何在不将输出分配给变量的情况下,直接在流程的返回代码上使用
if

$ cat is_cmd_running.sh 
#!/bin/bash
if pgrep "$1" >/dev/null ; then
  echo "$1 is running"
else
  echo "$1 is not running"
fi
$ shellcheck is_cmd_running.sh
$ echo $?
0
$

:)

是的,如果括号中的语法有误,您可以使用
[
[[
但两者后面都需要空格(有关经典测试程序的更多信息,请参见
man[

但是,我假设您希望它不仅仅适用于
crond
,所以我将它制作成一个带有arg的脚本

$ cat is_cmd_running.sh 
#!/bin/bash

psout=$(ps -ef | grep -v grep | grep -v $0 | grep $1) 
if [[ -z ${psout:-} ]]; then
  echo "$1 is not running"
else
  echo "$1 is running"
fi
请注意,除了过滤器
-v grep
,我还必须过滤
-v$0
,否则它会自动匹配程序的ps。此程序在我的系统上工作:

$ bash is_cmd_running.sh firefox
firefox is running
$ bash is_cmd_running.sh konqueror
 konqueror is not running
还有一个更简单的版本,您不需要测试变量,只需计算上一个
grep
的返回代码。通常的做法是使用
-q
标志使grep安静,然后只计算返回代码。我认为,在shell脚本中,这是更传统的做法:

$ cat is_cmd_running.sh 
#!/bin/bash

if ps -ef | grep -v grep | grep -v $0 | grep -q $1 ; then
  echo "$1 is running"
else
  echo "$1 is not running"
fi
这也适用于:

$ bash is_cmd_running.sh konqueror
konqueror is not running
$ bash is_cmd_running.sh firefox
firefox is running

编辑:

@tripleee很好地说明了标准命令的重新实现和linter的重要性(谢天谢地,我安装了
shellcheck
)因此,我提供了一个最终版本,该版本被
shellcheck
接受,它演示了如何在不将输出分配给变量的情况下,直接在流程的返回代码上使用
if

$ cat is_cmd_running.sh 
#!/bin/bash
if pgrep "$1" >/dev/null ; then
  echo "$1 is running"
else
  echo "$1 is not running"
fi
$ shellcheck is_cmd_running.sh
$ echo $?
0
$

:)

grep-v grep
-事实上,如果你要在
ps-ef
的输出上运行
|grep
,你会发现
grep
ps-ef
的输出中,我认为删除它很可爱。在这里发布代码之前先学会使用;-)。当你使用shellcheck时,你需要o包括一个适当的“她砰砰”行作为第一行,通常是
#!/bin/bash
。通过多次阅读避免前10个shell脚本初学者的错误。祝你好运。
grep-v grep
-你不需要这个事实上,如果你要在
ps-ef
的输出上运行
grep
,你会发现
grep
的输出中e> ps-ef,我认为删除它很可爱。在这里发布代码之前先学会使用;-)。当你使用shellcheck时,你需要包括一个合适的“she bang”行作为第一行,通常是
#!/bin/bash
。通过多次阅读避免前10个shell脚本初学者错误。祝您好运。仍然无法进入“if”条件:./test.sh spidmd07 3034 4649 0 2017?00:00:00 crond root 4649 1 0 2017?00:00:02 crond spidmd01 5477 4649 0 2017?00:00 crond。/test.sh:line 6:if[-n spidmd07 3034 4649 0 2017?00:00:00 crond root 4649 1 0 2017?00:00:02 crond spidmd01 5477 4649 0 2017?00:00:00 crond]:找不到命令。/test.sh:第7行:意外标记附近的语法错误
then./test.sh:第7行:
then'@riteshtakur,当我在Linux系统中检查时,它工作正常。您可以通过执行
cat-v test.sh
一次来检查您的系统上是否没有垃圾字符吗?还可以尝试在开始时放置
set-x
还有剧本。让我知道进展如何。有一个