首先我们使用A机器192.168.40.152 进行管理服务器 B机器192.168.40.142 C机器192.1683.40.167 两台服务器上有tomcat:/data/server/apache-tomcat/apache-tomcat8881/
我们采用自动登录将A机器上的tomcatRestat.sh tomcat执行启动停止的脚本远程自动传输到 B机器和C机器上、首先每次先登录B/C机器进行删除home目录的tomcatRestat.sh文件
,实现自动传输最新的tomcatRestat.sh文件到B/C机器的home目录下,然后根据Tomat的脚本启动状态判断,如果tomcat已经启动了,那么将停止tomcat、反之tomcat停止状态就把tomcat启动,并输出服务器的ip + tomcat的相关操作日志
当前服务器的文件目录及会用到的所有文件
pwd
/home/hanson/hsshell/sysLoginSh
ll
cpExeShell.sh,expect_ssh.sh,host.txt,mvExeShell.sh,rmExeShell.sh,scpExeShell.sh,testRestartTomcat.sh,tomcatRestat.sh
服务器配置信息文件host.txt
B机器:192.168.40.142 123456
C机器:192.168.40.167 123456
删除B/C服务器原有的home目录下的tomcatRestat.sh文件的执行文件mvExeShell.sh
#!/bin/bash
dir=/home/hanson/hsshell/sysLoginSh
while read line
do
host=`echo $line| awk '{print $1}'`
passwd=`echo $line | awk '{print $2}'`
$dir/rmExeShell.sh $host $passwd &
done < $dir/host.txt
执行删除远程服务器B/C的shell文件rmExeShell.sh
#!/usr/bin/env expect
set HOST [lindex $argv 0]
set PASSWD [lindex $argv 1]
spawn ssh root@$HOST
expect "(yes/no)?" { send "yes\n" }
expect "*password:" { send "$PASSWD\n" }
expect "*password:" { send "$PASSWD\n" }
expect "*#" { send "rm -rf /home/tomcatRestat.sh\n" }
expect eof
查看B/C服务器的home目录下的tomcatRestart.sh文件
[root@localhost bin]# ll /home/tomcatRestat.sh
ls: cannot access /home/tomcatRestat.sh: No such file or directory
下面我们执行将最新的tomcatRestart.sh文件传输到B/C服务器的home目录下
使用到scpExeShell.sh
#!/bin/bash
dir=/home/hanson/hsshell/sysLoginSh
while read line
do
host=`echo $line| awk '{print $1}'`
passwd=`echo $line | awk '{print $2}'`
$dir/cpExeShell.sh $host $passwd &
done < $dir/host.txt
执行复制的具体shell脚本文件cpExeShell.sh
#!/usr/bin/env expect
set HOST [lindex $argv 0]
set PASSWD [lindex $argv 1]
spawn scp /home/hanson/hsshell/sysLoginSh/tomcatRestat.sh root@$HOST:/home
expect "*password:" { send "$PASSWD\n" }
expect eof
执行远程传输B/C服务器home目录结果如下
[root@localhost bin]# ll /home/tomcatRestat.sh
-rwxr-xr-x. 1 root root 1270 Aug 23 04:09 /home/tomcatRestat.sh
具体的tomcat执行启动停止脚本都已经准备到位了,B/C服务器上的tomcat8881也已经存在相应的目录/data/server/apache-tomcat/apache-tomcat8881
下面继续介绍远程停止启动tomcat的具体执行脚本
测试调用脚本testRestartTomcat.sh
#!/bin/bash
dir=/home/hanson/hsshell/sysLoginSh
while read line
do
host=`echo $line| awk '{print $1}'`
passwd=`echo $line | awk '{print $2}'`
$dir/expect_ssh.sh $host $passwd &
done < $dir/host.txt
执行登录调用tomcat控制的脚本文件expect_ssh.sh
#!/usr/bin/env expect
set HOST [lindex $argv 0]
set PASSWD [lindex $argv 1]
spawn ssh root@$HOST
expect "(yes/no)?" { send "yes\n" }
expect "*password:" { send "$PASSWD\n" }
expect "*password:" { send "$PASSWD\n" }
#expect "*#" { send "cd /data/server/apache-tomcat/apache-tomcat8881/bin\n" } #执行相关操作,比如adduser
expect "*#" { send "/home/tomcatRestat.sh\n" } #执行相关操作,比如adduser
expect eof
最后为大家献上tomcat具体的控制脚本tomcatRestat.sh
#!/bin/bash
server_tomcat_path='/data/server/apache-tomcat/apache-tomcat8881'
ipaddresss=`ifconfig eth0 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'`
echo "当前服务器IP地址:" $ipaddresss
cd $server_tomcat_path/bin
running=$(ps -ef |grep `pwd` |grep -v grep)
tomcatPid=$(ps -ef |grep `pwd` |grep -v grep | awk '{print $2}')
echo $ipaddresss "tomcat进程号"$tomcatPid
echo "当前状态:"$running
if [ "x" != "x$running" ]
then
echo $ipaddresss "Tomcat running!---执行停止操作"
$(ps -ef |grep `pwd` |grep -v grep | awk '{print $2}'| xargs kill -9)
sleep 3
running=$(ps -ef |grep `pwd` |grep -v grep)
if [ "x" != "x$running" ]
then
echo "Tomcat 停止失败 error!"
exit 1
else
echo "Tomcat 停止成功 succeed"
exit 0
fi
exit 0
else
echo $ipaddresss "Tomcat stoping----执行启动操作"
su - -c "$server_tomcat_path/bin/startup.sh &"
sleep 3
running=$(ps -ef |grep `pwd` |grep -v grep)
if [ "x" = "x$running" ]
then
echo $ipaddresss "Tomcat 重启失败 error!"
exit 1
else
echo $ipaddresss "Tomcat 重启成功 succeed"
exit 0
fi
fi
具体的执行输出截图如下