我在一台电脑上使用 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