简体   繁体   English

检查服务是否通过bash脚本运行

[英]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.

相关问题 将bash脚本作为服务运行并写入另一个bash脚本不起作用 - Running bash script as a service and write to another bash script is not working Bash脚本检查特定的php进程是否正在运行? - Bash script to check a specific php process is running? Bash脚本检查多个正在运行的进程 - Bash script to check multiple running processes 通过 bash 脚本将参数传递给正在运行的后台 java 进程 - Pass params to a running background java process via bash script 检查从同一 bash 脚本启动的后台进程的运行状态 - Check running status of a background process launched from same bash script 检查bash脚本(由cron作业运行)是否在Openshift中运行 - Check if bash script (run by cron job) is running in Openshift Bash脚本检查进程是否正在运行,并在必要时读取用户输入以启动 - Bash script to check if process is running and read user input to start if necessary 如何在不运行 Bash 脚本的情况下对其进行语法检查? - How do I syntax check a Bash script without running it? 如何使用 bash 脚本检查主管进程是否正在运行或停止 - How to check if supervisor process is running or stopped using bash script 如何通过bash查询Linux Mint中运行bash脚本的虚拟桌面数量? - How can I query the number of the virtual desktop on which the bash script is running in Linux Mint via bash?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM