ansible-playbook 远程启动程序ansible结束后程序也被关闭,


ansible-playbook 远程启动程序

远程启动程序后ansible执行成功后程序被关闭

今天写个ansible-playbook部署war,unarchive包时很正常就是在重启tomcat的脚本怎么也启动不起来,后来经过测试发现tomcat有启动,因为公司tomcat没有做成服务,所以得使用tomcat脚本去启动,例:(/home/a/app/tomcat/bin/catalina.sh start) 记得要把bin目录下面所有sh都授成可执行权限(chmod u+x *.sh),但是当ansible正常退出时tomcat进程被关闭,怀疑是ansible fork出来子线程来运行脚本,ansible正常退出时会结束所有fork的子线程,这设计也是对的,解决方法在下面

解决方法

因为tomcat启动后被关闭,想到是不是让tomcat在后台运行就可以,后经过测试想法是正常的解决方法,代码如下:


nohup su - fuck /home/a/tomcat/bin/catalina.sh start 1>2& &

重启tomcat 脚本(python,shell)

python


import os
import sys
import time

tomcat_path = sys.argv[1]
tomcat_user = sys.argv[2]

if not os.path.exists(tomcat_path):
print(“tomcat目录不存在”)
sys.exit(0)

index = 0

def get_tomcat_pid():
return os.popen(“ps axu|grep tomcat|grep -v grep |grep \”%s\”|grep -v python|awk ‘{print $2}’” % tomcat_path).read()

def stop_tomcat(index=0):
temp_tomcat_pid = get_tomcat_pid()
print(“temp_tomcat_pid:”, temp_tomcat_pid)
if not temp_tomcat_pid:
return 0
pid = int(temp_tomcat_pid.replace(“\n”, “”).strip())

if index <= 9:
    return pid

time.sleep(1)
index += 1

if int(pid) > 0:
    os.popen("%s/%s" % (tomcat_path, "bin/catalina.sh stop"))
    stop_tomcat()

tomcat_pid = stop_tomcat()

def force_kill_tomcat():
os.popen(“kill -9 %s” % tomcat_pid)

def start_tomcat():
os.popen(“nohup su - %s %s%s 1>&2 &” % (tomcat_user, tomcat_path, “bin/catalina.sh start”))
time.sleep(5)
return os.popen(“tail -n 100 %s” % (tomcat_path + “logs/catalina.out”)).read()

if tomcat_pid > 0:
print(“强制停止tomcat”)
force_kill_tomcat()

start_tomcat()

shell


tomcat_path=$1
tomcat_user=$2

if [ ! -e “$tomcat_path” ]; then
echo “tomcat path not exists”
exit 0
fi

function get_tomcat_pid()
{
path=1return(ps axu|grep tomcat|grep -v grep |grep "path|grepv0"|awk '{printf $2}’)
}

function exec_stop_tomcat()
{
$tomcat_path/bin/catalina.sh stop
}

function stop_tomcat()
{
for i in {0..9};
do
get_tomcat_pid tomcatpathtemppid=?
if [[ -z “temppid"]]||[[temp_pid -eq 0 ]]; then
return 0
fi
if [[ -n “temp_pid" ]] && [[temp_pid -gt 0 ]] && [[ temppideq9]];thenreturntomcat_pid
fi
exec_stop_tomcat
done
}

stop_tomcat
tomcat_pid=?echotomcatpid:tomcat_pid

function start_tomcat()
{
nohup su - tomcatusertomcat_path/bin/catalina.sh start 1>&2 &
sleep 5
tail -n 100 $tomcat_path/logs/catalina.out
}

if [[ tomcatpidgt0]];thenkill9tomcat_pid
fi

start_tomcat

相关内容

    暂无相关文章