推荐学习书目
› 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 · May 17, 2016 · 3257 views
    This topic created in 3793 days ago, the information mentioned may be changed or developed.
    我在一台电脑上使用 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
       May 17, 2016
    已经解决了, run('service tomcat restart',pty=False)
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   741 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 20:58 · PVG 04:58 · LAX 13:58 · JFK 16:58
    ♥ Do have faith in what you're doing.