Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 检查mosquitto是否正常的脚本_Bash_Docker_Mosquitto - Fatal编程技术网

Bash 检查mosquitto是否正常的脚本

Bash 检查mosquitto是否正常的脚本,bash,docker,mosquitto,Bash,Docker,Mosquitto,我正在尝试为mosquitto创建一个健康检查脚本(docker使用) 我遇到的问题是,如果给mosquitto_sub的密码不正确,它只会不断输出连接被拒绝:未授权。一次又一次,docker中的超时内容看起来像雪花,所以它永远不会结束 看起来mosquitto并没有让失败变得更好。我想我可能需要把它作为一个后台进程来执行,我可以杀死它,但是我的bash并没有那么好,所以有人有更好的想法吗 [编辑-根据BMitch的建议进行更新] 我已将脚本修改为如下所示: #!/bin/sh if [ -z

我正在尝试为mosquitto创建一个健康检查脚本(docker使用)

我遇到的问题是,如果给mosquitto_sub的密码不正确,它只会不断输出
连接被拒绝:未授权。
一次又一次,docker中的超时内容看起来像雪花,所以它永远不会结束

看起来mosquitto并没有让失败变得更好。我想我可能需要把它作为一个后台进程来执行,我可以杀死它,但是我的bash并没有那么好,所以有人有更好的想法吗

[编辑-根据BMitch的建议进行更新]

我已将脚本修改为如下所示:

#!/bin/sh

if [ -z "$USERNAME" ]; then
        (sleep 10; kill $$) & exec mosquitto_sub -t '$SYS/#' -C 1 | grep -v Error || exit 1 "$@"
else
        (sleep 10; kill $$) & exec mosquitto_sub -t '$SYS/#' -u $USERNAME -P $PASSWORD -C 1 | grep -v Error || exit 1  "$@"
fi
但运行它只会产生以下输出:

Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.
Terminated
root@e30e9cadd8fc:/# Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.
有一个关于如何超时运行脚本的问题。尝试用包含以下内容的mosquitto_sub_timeout.sh替换mosquitto_sub:

#!/bin/bash
(sleep 10; kill $$) & exec mosquitto_sub "$@"
然后,您的healthcheck脚本将如下所示:

if [ -z "$USERNAME" ]; then
 mosquitto_sub_timeout.sh -t '$SYS/#' -C 1 | grep -v Error || exit 1
else 
 mosquitto_sub_timeout.sh -t '$SYS/#' -C 1 -u $USERNAME -P $PASSWRD | grep -v Error || exit 1
fi

最后一个更新,如bash常见问题解答中所述,timeout命令可能是最好的解决方案,只要它安装在您的容器中:

if [ -z "$USERNAME" ]; then
 timeout --foreground 10 mosquitto_sub -t '$SYS/#' -C 1 | grep -v Error || exit 1
else 
 timeout --foreground 10 mosquitto_sub -t '$SYS/#' -C 1 -u $USERNAME -P $PASSWRD | grep -v Error || exit 1
fi
有一个关于如何超时运行脚本的问题。尝试用包含以下内容的mosquitto_sub_timeout.sh替换mosquitto_sub:

#!/bin/bash
(sleep 10; kill $$) & exec mosquitto_sub "$@"
然后,您的healthcheck脚本将如下所示:

if [ -z "$USERNAME" ]; then
 mosquitto_sub_timeout.sh -t '$SYS/#' -C 1 | grep -v Error || exit 1
else 
 mosquitto_sub_timeout.sh -t '$SYS/#' -C 1 -u $USERNAME -P $PASSWRD | grep -v Error || exit 1
fi

最后一个更新,如bash常见问题解答中所述,timeout命令可能是最好的解决方案,只要它安装在您的容器中:

if [ -z "$USERNAME" ]; then
 timeout --foreground 10 mosquitto_sub -t '$SYS/#' -C 1 | grep -v Error || exit 1
else 
 timeout --foreground 10 mosquitto_sub -t '$SYS/#' -C 1 -u $USERNAME -P $PASSWRD | grep -v Error || exit 1
fi
(改变观念)

我不得不将超时参数从
--foreground
更改为
-t
,并在
$
前面加上一个
$
符号,以转义
$

在my docker compose it look next(请记住用户名和密码已删除,您仍然可以添加它们-只需再次退出
$
符号):

版本:“3”
服务:
莫斯基托:
图片:“EclipseMosquitto:1.6.7”
集装箱名称:莫斯奎托
主机名:mosquitto
卷数:
-./mosquitcho/data:/mosquitcho/data
-./mosquitto/log:/mosquitto/log
-./mosquitcho/config:/mosquitcho/config
网络:
-簇
重新启动:失败时
健康检查:
测试:[“CMD-SHELL”,“timeout-t5 mosquitto_sub-t'$$SYS/#'-c1 | grep-v Error | | exit 1”]
间隔时间:10秒
超时时间:10秒
重试次数:6次

(改变了观念)

我不得不将超时参数从
--foreground
更改为
-t
,并在
$
前面加上一个
$
符号,以转义
$

在my docker compose it look next(请记住用户名和密码已删除,您仍然可以添加它们-只需再次退出
$
符号):

版本:“3”
服务:
莫斯基托:
图片:“EclipseMosquitto:1.6.7”
集装箱名称:莫斯奎托
主机名:mosquitto
卷数:
-./mosquitcho/data:/mosquitcho/data
-./mosquitto/log:/mosquitto/log
-./mosquitcho/config:/mosquitcho/config
网络:
-簇
重新启动:失败时
健康检查:
测试:[“CMD-SHELL”,“timeout-t5 mosquitto_sub-t'$$SYS/#'-c1 | grep-v Error | | exit 1”]
间隔时间:10秒
超时时间:10秒
重试次数:6次

这似乎不起作用。我把它放在顶部,但它仍然只是说连接被拒绝,直到我最终按下ctrl+c组合键。可能是因为
mosquitto\u sub
将是一个单独的进程,不会被发送到父shell的信号杀死,我将用一个潜在的解决方法来调整答案,但是bash常见问题解答要彻底得多。这不起作用-我更新了原始问题以反映我所做的更改。感谢您的帮助。第二个shell脚本非常重要,它包含一个与父shell脚本分离的exec。否则exec将中断您的grep和其他命令。单独终止的错误使我认为它正在生成其他命令,您可能需要一个
killall mosquitto_xyz
来启动一些打开tty的命令。我还更新了timeout命令,它可能比任何bash脚本选项都好。这似乎不起作用。我把它放在顶部,但它仍然只是说连接被拒绝,直到我最终按下ctrl+c组合键。可能是因为
mosquitto\u sub
将是一个单独的进程,不会被发送到父shell的信号杀死,我将用一个潜在的解决方法来调整答案,但是bash常见问题解答要彻底得多。这不起作用-我更新了原始问题以反映我所做的更改。感谢您的帮助。第二个shell脚本非常重要,它包含一个与父shell脚本分离的exec。否则exec将中断您的grep和其他命令。单独终止的错误使我认为它正在生成其他命令,您可能需要一个
killall mosquitto_xyz
来启动一些打开tty的命令。我还更新了timeout命令,它可能比任何bash脚本选项都好。