ansible shell 之运行后台程序

Posted lihuanhuan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ansible shell 之运行后台程序相关的知识,希望对你有一定的参考价值。

最近在使用ansible shell模块启动一个shell编写的脚本,该脚本主要功能式加载java的classpath并在后台运行这个java程序。
该脚本在linux shell中可以正常启动和停止,但是使用ansible shell模块却每次都启动后进程都消失了,日志没有任何异常,pid文件也生成了。
后来经过一个同事的猜想,是否有程序将该进程kill掉了。

于是产生了以下几种猜想:
1.ansible shell模块执行完shell脚本,就立即关闭当前的shell,进程也就被关闭了。
ansible fork出来子线程来运行脚本,ansible正常退出时会结束所有fork的子线程因为程序启动后被关闭.

尝试解决: 给shell命令开头加个nohup, 结尾加个&, 如果环境变量找不到,加入source ~/.bash_profile
例如:source ~/.bash_profile;nohup /apps/xxx/ss/start.sh &

参考:
Linux set命令参数及用法详解 (https://www.cnblogs.com/liduanjun/p/3536996.html
shell执行startup.sh无法启动tmocat的问题 (https://segmentfault.com/q/1010000010375831)
ansible启动tomcat失败 (https://www.zhihu.com/question/52712638)
ansible在远程机器将程序起在后台 (https://blog.csdn.net/cikenerd/article/details/54411633)
Ansible常用模块:cron、synchronize、shell、service (https://blog.csdn.net/lcl_xiaowugui/article/details/81905324)
关于ansible远程执行的环境变量问题(login shell & nonlogin shelll)(https://blog.csdn.net/u010871982/article/details/78525367)

以上是关于ansible shell 之运行后台程序的主要内容,如果未能解决你的问题,请参考以下文章

[转]-nohup-真正的Shell后台运行

shell基础之管道符和变量

linux后台运行jar程序

劣质 Shell 或 UIOP:与后台进程交互

nohup 后台不挂断运行程序

linux中启动 java -jar 后台运行程序