如何通过paraniko或fabric在另一台服务器上运行服务脚本?

时间:2016-05-16 09:59:23

标签: python fabric paramiko

我在电脑上编写了一个结构脚本( 也是由paramiko测试的,远程重启一个tomcat服务器,当我发现show的结果已经开始运行脚本时,实际上没有成功启动。

结构脚本:

# _*_ coding:utf-8 _*_
from fabric.api import *

env.user = 'root'
env.hosts = [
    '192.168.1.72'
]
env.password = 'root'


@task
def start():
    run('service tomcat status')
    run('service tomcat restart')
    run('service tomcat status')

运行结果,你可以看到,两个打印的tomcat状态没有运行

[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.

tomcat服务脚本

#!/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
有人曾经告诉我,每次远程执行都是远程自动生成一个新的终端,当终端退出时,也拔出了我的脚本,所以执行失败了。我建议使用nohup后台,但是我发现无论使用nohup执行Fabric后台的脚本,还是在任务运行里面使用nohup,都不要生效

0 个答案:

没有答案