linux - 通过 bash 脚本检查服务是否正在运行

标签 linux bash shell ubuntu unix

我有一个脚本sh startAWS.sh。 最重要的是,我检查了这一点:

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

当我运行时,我得到了

⚡️  laravel  sh startAWS.sh                                                            
You need to start your Postgres service to run this script.                           
       6  

嗯...什么...🤦🏻‍♂️

我的 Postgres 正在运行,不应执行 echo。

显示6,并且6不小于1,该代码不应被执行。

有谁知道为什么我的 echo 打印出来了?

最佳答案

您需要执行数字比较,因此在检查中使用 -lt 或 -gt。

Comparing numbers in Bash

if [[ `ps -acx|grep postgres|wc -l` -lt 1 ]];

关于linux - 通过 bash 脚本检查服务是否正在运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55172819/

相关文章:

bash - sed ... <(cmd) 和 cmd | 的替代方案sed

linux - 排除目录并删除旧备份

linux - 如何解析输出并将其存储到变量中

linux - Bash 多变量赋值

c++ - osx/win7/linux上不同的c++调用转换

linux - 我如何通过文本文件编写 hunpos 脚本?

linux - 尝试使 shell 脚本可执行时出错

shell - 如何在 tcsh shell 上映射 Delete 和 End 键?

c++ - Ubuntu Conda环境下无法编译R包: x86_64-conda-linux-gnu-c++: not found

c - 局部变量未在内联函数中对齐