V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
zxd
V2EX  ›  Python

如何使用 paramiko 或者 fabric 执行远程服务器上的 service 启动脚本?

  •  
  •   zxd · 2016-05-17 10:30:33 +08:00 · 2731 次点击
    这是一个创建于 2907 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我在一台电脑上使用 paramiko 或者 fabric 去运行远程服务器上的 service 脚本启动 tomcat ,该脚本直接在远程服务器上执行 service tomcat start ,是可以正常执行的,但是在 paramiko 或者 fabric 中执行的时候,回显是显示正常执行了,打印出了 pid ,随后执行 service tomcat status 显示的状态是 not running ,去远程服务器上查看也是未执行。

    fabric 脚本如下:
    # _*_ coding:utf-8 _*_
    from fabric.api import *
    env.user = 'root'
    env.hosts = [
    '192.168.1.72'
    ]
    env.password = 'root'
    @task
    def server():
    run('service tomcat status')
    run('service tomcat restart')
    run('service tomcat status')

    执行结果如下:
    [192.168.1.72] Executing task 'start'
    [192.168.1.72] run: service tomcat status
    [192.168.1.72] out: Tomcat is not running
    [192.168.1.72] out:

    [192.168.1.72] run: service tomcat restart
    [192.168.1.72] out: Tomcat is not running
    [192.168.1.72] out: Starting tomcat
    [192.168.1.72] out: Using CATALINA_BASE: /usr/local/tomcat
    [192.168.1.72] out: Using CATALINA_HOME: /usr/local/tomcat
    [192.168.1.72] out: Using CATALINA_TMPDIR: /usr/local/tomcat/temp
    [192.168.1.72] out: Using JRE_HOME: /usr/java/latest
    [192.168.1.72] out: Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
    [192.168.1.72] out: Tomcat started.
    [192.168.1.72] out: Tomcat is running with pid: 41687
    [192.168.1.72] out:

    [192.168.1.72] run: service tomcat status
    [192.168.1.72] out: Tomcat is not running
    [192.168.1.72] out:


    Done.
    Disconnecting from 192.168.1.72... done.

    可以看到,执行了 service tomcat restart 之后,再次执行 service tomcat status ,结果依然是 not running 。

    tomcat service 脚本如下:
    #!/bin/bash
    #
    # chkconfig: - 85 15
    # description: Tomcat start/stop/status script

    # 包含函数库
    . /etc/rc.d/init.d/functions

    # 获取网络配置
    . /etc/sysconfig/network

    # 检测 NETWORKING 是否为 "yes"
    [ "${NETWORKING}" = "no" ] && exit 0

    #Location of JAVA_HOME (bin files)
    export JAVA_HOME=/usr/java/latest

    #Add Java binary files to PATH
    export PATH=$JAVA_HOME/bin:$PATH

    #CATALINA_HOME is the location of the configuration files of this instance of Tomcat
    TOMCAT_HOME=/usr/local/tomcat

    #TOMCAT_USAGE is the message if this script is called without any options
    TOMCAT_USAGE="Usage: $0 {\e[00;32mstart\e[00m|\e[00;31mstop\e[00m|\e[00;32mstatus\e[00m|\e[00;31mrestart\e[00m}"

    #SHUTDOWN_WAIT is wait time in seconds for java proccess to stop
    SHUTDOWN_WAIT=10

    tomcat_pid() {
    echo `ps -ef | grep $TOMCAT_HOME | grep -v grep | tr -s " "|cut -d" " -f2`
    }

    start() {
    pid=$(tomcat_pid)
    if [ -n "$pid" ];then
    echo -e "\e[00;31mTomcat is already running (pid: $pid)\e[00m"
    else
    echo -e "\e[00;32mStarting tomcat\e[00m"
    $TOMCAT_HOME/bin/startup.sh
    fi
    status
    }

    status(){
    pid=$(tomcat_pid)
    if [ -n "$pid" ];then
    echo -e "\e[00;32mTomcat is running with pid: $pid\e[00m"
    else
    echo -e "\e[00;31mTomcat is not running\e[00m"
    fi
    }

    stop() {
    pid=$(tomcat_pid)
    if [ -n "$pid" ];then
    echo -e "\e[00;31mStoping Tomcat\e[00m"
    $TOMCAT_HOME/bin/shutdown.sh
    let kwait=$SHUTDOWN_WAIT
    count=0;
    until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
    do
    echo -n -e "\e[00;31mwaiting for processes to exit\e[00m\n";
    sleep 1
    let count=$count+1;
    done

    if [ $count -gt $kwait ];then
    echo -n -e "\n\e[00;31mkilling processes which didn't stop after $SHUTDOWN_WAIT seconds\e[00m"
    kill -9 $pid
    fi
    else
    echo -e "\e[00;31mTomcat is not running\e[00m"
    fi

    return 0
    }

    case $1 in
    start)
    start
    ;;

    stop)
    stop
    ;;

    restart)
    stop
    start
    ;;

    status)
    status
    ;;

    *)
    echo -e $TOMCAT_USAGE
    ;;
    esac
    exit 0
    zxd
        1
    zxd  
    OP
       2016-05-17 11:08:44 +08:00
    已经解决了, run('service tomcat restart',pty=False)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2239 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 07:39 · PVG 15:39 · LAX 00:39 · JFK 03:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.