linux使用shell脚本管理其他服务器Tomcat启动停止、自动发布
2017-08-23 19:21
771 查看
首先我们使用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!--
aeb5
-执行停止操作"
$(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
具体的执行输出截图如下
我们采用自动登录将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!--
aeb5
-执行停止操作"
$(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
具体的执行输出截图如下
相关文章推荐
- Linux crontab的使用方式,sh脚本的编写,sh脚本自动启动tomcat服务器,sh监控系统运行情况
- linux使用shell脚本实现自动登录scp传输文件到其他服务器
- Linux crontab的使用方式,sh脚本的编写,sh脚本自动启动tomcat服务器,sh监控系统运行情况
- linux下用脚本监测tomcat停止并且自动启动
- linux环境在tomcat启动或者停止后执行shell脚本,脚本内容备份图片文件夹
- Linux下使用shell脚本自动监控重启tomcat
- linux环境下编写shell脚本实现启动停止tomcat服务的方法
- Centos开机自动执行shell脚本启动tomcat服务器
- Linux下使用shell脚本自动监控重启tomcat
- 用php写的进程守护,进程管理,进程出错自动启动功能,适合服务器管理员使用
- 使用交互式shell脚本启动DHCP服务器以及对其进行设置
- LINUX下使用Shell自动监控tomcat并且执行重启操作
- 使用Shell脚本自动拒绝某个国家/地区对服务器的访问
- 启动Oracle报错:本地计算机上的OracleOraDb11g_homeTNSListener服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止。
- PeopleSoft 服务器管理 - 通过脚本启动/停止服务器
- Linux下,Oracle自动启动,停止脚本 (转载)
- Linux下使用Shell脚本实现ftp的自动上传下载的代码小结
- Linux下使用Shell脚本向FTP服务器上传或下载一个文件
- 使用ANT脚本编译、打包、部署到tomcat,启动、停止tomcat
- Redhat RHEL Tomcat init script(tomcat在linux下自动启动初始化脚本)