美文网首页
Python2使用fabric远程启动不了tomcat

Python2使用fabric远程启动不了tomcat

作者: testerzhang | 来源:发表于2021-04-09 09:48 被阅读0次

这文章诞生的来源

原来今天看到一个帖子,然后说用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,原创技术文章第一时间推送。

相关文章

  • Python2使用fabric远程启动不了tomcat

    这文章诞生的来源 原来今天看到一个帖子,然后说用python启动不了tomcat。我猜想应该是远程调用启动不了。因...

  • Tomcat 开启远程Debug调试

    1. 使用外部Tomcat启动方式开启远程Debug调试功能 2. SpringBoot 项目开启远程Debug:...

  • Tomcat环境变量配置

    想要Tomcat服务正常启动,自然是少不了相关环境变量的正确配置,不然就会出现 Tomcat启动失败无法正常使用。...

  • fabric2.0 使用说明

    fabric2.0 使用说明 fabric框架,主要的目的就是用来远程自动化部署。在最近,作者将fabric框架重...

  • tomcat部署为服务

    使用命令 service tomcat start 启动tomcat报错 然后,使用命令 su - tomcat ...

  • jconsole远程连接

    jconsole 远程连接 war包启动 tomcat/bin下catelina.sh 中增加启动参数如下 ip,...

  • Tomcat常见问题

    Tomcat找不到JRE,启动不了JVM 启动Tomcat,windows环境会去找到JAVA_HOME变量,这样...

  • idea怎么用tomcat启动服务器

    当使用idea IDE,并配置tomcat时怎么启动服务呢? 直接使用下载下来的tomcat启动吗? 如果这样的,...

  • IDEA利用JPDA远程调试运行在tomcat中的程序

    远程调试分为两个主要步骤:1.启动远端Tomcat并开启一个远程调试端口2.本地IDE链接远端Tomcat 第一步...

  • Intellij IDEA远程调试Tomcat

    1.创建work账号并安装tomcat root启动的tomcat进程不支持远程调试 ,因此新建一个work账号 ...

网友评论

      本文标题:Python2使用fabric远程启动不了tomcat

      本文链接:https://www.haomeiwen.com/subject/gkqikltx.html