[英]check if service is running via bash script
I have a script sh startAWS.sh
. 我有一个脚本sh startAWS.sh
。 On top of it, i checked this : 最重要的是,我检查了这个:
if [[ `ps -acx|grep postgres|wc -l` < 1 ]]; then
echo "You need to start your Postgres service to run this script."
ps -acx|grep postgres|wc -l
exit 0
fi
When I run , I got 当我跑步时,我得到了
⚡️ laravel sh startAWS.sh
You need to start your Postgres service to run this script.
6
Hmm... what... 🤦🏻♂️ 嗯...什么...🤦🏻️
My Postgres is running, that echo should not be executed. 我的Postgres正在运行,不应执行该回显。
It shows 6
, and 6 is not less than 1, that code should not be executed. 它显示6
,并且6不小于1,不应执行代码。
Does anyone know why I echo is printing out? 有谁知道我为什么要回显打印?
You need to perform a numeric comparison so use -lt or -gt in your check. 您需要执行数字比较,因此在检查中使用-lt或-gt。
Comparing numbers in Bash 在Bash中比较数字
if [[ `ps -acx|grep postgres|wc -l` -lt 1 ]];
There are a couple of problems here. 这里有两个问题。
First, as @RameshNaidu pointed out, inside [[ < ]]
is doing string comparison (ie alphabetical order) rather than numeric, and that's causing trouble. 首先,正如@RameshNaidu指出的那样, [[ < ]]
内部正在进行字符串比较(即字母顺序)而不是数字,这会造成麻烦。 The usual problem this mistake causes is that string sorting order is different than numeric order -- for instance, [[ 10 < 2 ]]
evaluates to true, because "1" comes before "2" in character sorting order. 此错误导致的常见问题是字符串排序顺序与数字顺序不同-例如, [[ 10 < 2 ]]
计算结果为true,因为字符排序顺序中“ 1”位于“ 2”之前。 But that doesn't apply here, because "6" comes after "1" in sorting order as well. 但这在这里不适用,因为“ 6”在排序顺序上也排在“ 1”之后。 What's happening is subtler than that: wc -l
prints a number of spaces before the number of lines, and space does come before "1", so [[ " 6" < 1 ]]
evaluates to true. 发生的事情比这更妙: wc -l
在行数之前打印许多空格,并且空格确实在“ 1”之前,因此[[ " 6" < 1 ]]
计算结果为true。
Second, ps | grep something
第二, ps | grep something
ps | grep something
is a bad way to check whether a process is running, because (depending on the exact timing) the ps
command may include "grep something" in its output, and the grep
command will match that. ps | grep something
是检查进程是否正在运行的一种不好的方法,因为ps
命令(取决于确切的时间)可能在其输出中包括“ grep something”,而grep
命令将对此进行匹配。 If you have pgrep
available, use that instead, since it automatically avoids this problem. 如果有pgrep
可用,请改用它,因为它可以自动避免此问题。 Another common workaround is to use grep "[s]omething"
instead -- the brackets prevent it from matching itself, so you don't get this extra match. 另一个常见的解决方法是改用grep "[s]omething"
-方括号可防止其自身匹配,因此您不会获得额外的匹配。
But there's also a much simpler way to see if there were any matches: rather than using wc
to count matches, just use grep -q
(or pgrep -q
) to suppress output, and just check its exit status (success = found at least one match, so negate it with !
to check for no match). 但是,还有一种更简单的方法来查看是否存在匹配项:与其使用wc
来计算匹配项,不如使用grep -q
(或pgrep -q
)来抑制输出,并仅检查其退出状态(成功=至少找到了)一个匹配项,因此请用!
取消匹配以检查是否不匹配)。 It'll look like one of these: 看起来像其中之一:
if ! pgrep -q postgres; then
echo "You need to start your Postgres service to run this script."
or 要么
if ! ps -acx |grep -q "[p]ostgres"; then
echo "You need to start your Postgres service to run this script."
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.