Python2使用fabric远程启动不了tomcat

2021-04-09  本文已影响0人  testerzhang

这文章诞生的来源

原来今天看到一个帖子,然后说用python启动不了tomcat。我猜想应该是远程调用启动不了。因为以前我也碰到过,但是我之前解决过,在这里写个笔记分享下。

本机执行Python脚本

$ cat test.py 
#!/usr/bin/python
# coding=utf-8
# wx gzh: testerzhang

__author__ = 'testerzhang'

import os

os.system('cd /opt/testerzhang/tomcat7-test/bin;./startup.sh')

这样执行是可以的。

远程通过fabric执行,不行?

$ cat testerzhangdemo.py 
#!/usr/bin/python
# coding=utf-8
# wx gzh: testerzhang

__author__ = 'testerzhang'


from fabric.api import *
from fabric.context_managers import *
import time

def test_host():
    env.hosts=[
        'testerzhang@10.10.10.10:22',
    ]
    env.key_filename = "~/.ssh/id_rsa"


def restart_remote():
    print "remote restart"
    tomcatdir="/opt/testerzhang/tomcat7-test/bin"

    cmd='cd %s;sh -x ./stopmytomcat.sh' %(tomcatdir)
    #run(cmd)
    cmd='cd %s;sh ./startup.sh' %(tomcatdir)
    run(cmd)

执行:

$ fab -f testerzhangdemo.py test_host restart_remote
[testerzhang@10.10.10.10:22] Executing task 'restart_remote'
remote restart
[testerzhang@10.10.10.10:22] run: cd /opt/testerzhang/tomcat7-test/bin;sh ./startup.sh
[testerzhang@10.10.10.10:22] out: Using CATALINA_BASE:  /opt/testerzhang/tomcat7-test
[testerzhang@10.10.10.10:22] out: Using CATALINA_HOME:   /opt/testerzhang/tomcat7-test
[testerzhang@10.10.10.10:22] out: Using CATALINA_TMPDIR:/opt/testerzhang/tomcat7-test/temp
[testerzhang@10.10.10.10:22] out: Using JRE_HOME:        /opt/testerzhang/3rd/jdk1.8.0_161/jre
[testerzhang@10.10.10.10:22] out: Using CLASSPATH:  /opt/testerzhang/tomcat7-test/bin/bootstrap.jar:/opt/aiwm/dzsd/tomcat7-dzsd/bin/tomcat-juli.jar
[testerzhang@10.10.10.10:22] out: Tomcat started.
[testerzhang@10.10.10.10:22] out: 


Done.
Disconnecting from testerzhang@10.10.10.10... done.

然后你会以为启动tomcat成功 ,但是在10.10.10.10主机上却没有看到进程?

解决方案:

$ cat testerzhangdemo.py 
#!/usr/bin/python
# coding=utf-8
# wx gzh: testerzhang

__author__ = 'testerzhang'


from fabric.api import *
from fabric.context_managers import *
import time

def test_host():
    env.hosts=[
        'testerzhang@10.10.10.10:22',
    ]
    env.key_filename = "~/.ssh/id_rsa"


def restart_remote():
    print "remote restart"
    tomcatdir="/opt/testerzhang/tomcat7-test/bin"

    cmd='set -m;cd %s;sh -x ./stopmytomcat.sh' %(tomcatdir)
    #run(cmd)
    cmd='set -m;cd %s;sh ./startup.sh' %(tomcatdir)
    run(cmd)

执行:

$ fab -f testerzhangdemo.py test_host restart_remote
[testerzhang@10.10.10.10:22] Executing task 'restart_remote'
remote restart
[testerzhang@10.10.10.10:22] run: cd /opt/testerzhang/tomcat7-test/bin;sh ./startup.sh
[testerzhang@10.10.10.10:22] out: Using CATALINA_BASE:  /opt/testerzhang/tomcat7-test
[testerzhang@10.10.10.10:22] out: Using CATALINA_HOME:   /opt/testerzhang/tomcat7-test
[testerzhang@10.10.10.10:22] out: Using CATALINA_TMPDIR:/opt/testerzhang/tomcat7-test/temp
[testerzhang@10.10.10.10:22] out: Using JRE_HOME:        /opt/testerzhang/3rd/jdk1.8.0_161/jre
[testerzhang@10.10.10.10:22] out: Using CLASSPATH:  /opt/testerzhang/tomcat7-test/bin/bootstrap.jar:/opt/aiwm/dzsd/tomcat7-dzsd/bin/tomcat-juli.jar
[testerzhang@10.10.10.10:22] out: Tomcat started.
[testerzhang@10.10.10.10:22] out: 


Done.
Disconnecting from testerzhang@10.10.10.10... done.

然后这次你在目标机器可以看到tomcat正常启动了。

结束语

你们有兴趣可以去看看为啥set -m加进去就能生效的问题了。希望能帮助到需要的童鞋。

本文将同步自我的同名wx gzh,也欢迎大家关注。


欢迎关注我的公众号testerzhang,原创技术文章第一时间推送。

上一篇下一篇

猜你喜欢

热点阅读