首页 > paramiko,fabric执行远程服务器上的service命令启动tomcat失败?

paramiko,fabric执行远程服务器上的service命令启动tomcat失败?

我在一台电脑上使用paramiko、fabric、ansible去运行远程服务器上的service 脚本启动tomcat,该脚本直接在远程服务器上执行 service tomcat start,是可以正常执行的,但是在paramiko或者fabric中执行的时候,回显是显示正常执行了,随后打印的服务状态却是未执行,去远程服务器上查看也是未执行。

fabric脚本如下:`#!/usr/bin/env python

    # _*_ 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')`
        

执行结果如下:

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执行,不管是在run里用 nohup service tomcat start 还是用 nohup fab -f server.py start >/tmp/nohup.out 执行脚本,都是一样的结果,求各位帮忙解答疑惑。


用run('',pty=False)

pty在True的情况下启动tomcat正常,但会在结束后导致tomcat进程也被干掉

【热门文章】
【热门文章】