如何从Ansible-playbook的bash-script启动后台循环

问题描述 投票:0回答:1

我正在创建一个简单的流量模拟器:一个每10秒钟卷曲一次Web服务器的客户端。客户端(Debian)和WebServer使用Ansible进行配置。 Ansible-SSH连接关闭后,后台循环将关闭。

起初我启动了命令:

$ while true; do curl python_webserver:8000; sleep10; done </dev/null >/dev/null 2>&1 &;
$ disown

它可以在Bash中正常工作,但是如果我将它放入脚本中,它会在ssh-connection端退出。我尝试了一些其他解决方案,比如使用:

$ nohup [command]
$ nohup /bin/bash -c '[command]

或者使用“deamonize”但没有任何效果。我没有找到任何在线作品;也许我错过了一些重要的东西。 (写pid很重要,但不是根本)

我在这里的剧本,也许有一个很大的新手错误。

#!/bin/bash
PORT=8000
while true; do
    curl python_webserver:$PORT
    sleep 10
done >/dev/null 2>&1 &
ANSIBLE_CLIENT_PID=$!
echo $ANSIBLE_CLIENT_PID >> /tmp/ANSIBLE_PID.txt
disown
bash ansible
1个回答
0
投票

答案是

nohup sh -c 'while true; do curl python_webserver:'"$PORT"'; sleep 10; done >/dev/null 2>&1' &

可能我用引号搞错了。谢谢大家的帮助

(特别是Kamil Cuk的解决方案)

© www.soinside.com 2019 - 2024. All rights reserved.