前提需要ssh免密钥登录,具体操作详解ssh免密钥登录。
点击(此处)折叠或打开
- #!/bin/bash
- dest_ip_arr=(10.99.11.23)
- dest_lib_dir="/instance/filesevice/devstat_lib"
- dest_tomcat_dir="/instacne/filesevice/tomcat-file-show"
- src_jar="/instance/filesevice/devstat/devstat.war"
- port=22
- jar_xvf_cmd="cd $dest_lib_dir; /software/jdk1.7.0_65/bin/jar -xvf $dest_lib_dir/devstat.war"
- start_cmd="cd $dest_tomcat_dir/bin; /bin/sh startup.sh"
- stop_cmd="cd $dest_tomcat_dir/bin; /bin/sh shutdown.sh"
- get_pid_cmd="ps -ef |grep -w \"$dest_tomcat_dir\" |grep -v 'grep' |awk '{print $2}'"
- user=work
- function start() {
- for ip in ${dest_ip_arr[@]};
- do
- echo "start $ip $dest_tomcat_dir now"
- ssh -l$user -p$port $ip "$start_cmd"
- pid=`ssh -l$user -p$port $ip "$get_pid_cmd"`
- test "$pid" != '' && echo "start $ip $dest_tomcat_dir ok" || echo "start $ip $dest_tomcat_dir failed!"
- sleep 1
- done
- }
- function stop() {
- for ip in ${dest_ip_arr[@]};
- do
- echo "stop $ip $dest_tomcat_dir now"
- ssh -l$user -p$port $ip "$stop_cmd"
- pid=`ssh -l$user -p$port $ip "$get_pid_cmd"`
- test "$pid" != ''&& echo "stop $ip $dest_tomcat_dir ok!" || echo "stop $ip $dest_tomcat_dir ok!"
- sleep 1
- done
- }
- function status() {
- for ip in ${dest_ip_arr[@]};
- do
- pid=`ssh -l$user -p$port $ip "$get_pid_cmd"`
- test "$pid" != '' && echo "$ip $dest_tomcat_dir is running, and the pid is $pid" || echo "can not detect the pid of $ip $dest_tomcat_dir is !"
- done
- }
- function restart() {
- for ip in ${dest_ip_arr[@]};
- do
- echo "restart $ip $dest_tomcat_dir now"
- ssh -l$user -p$port $ip "$stop_cmd"
- pid=`ssh -l$user -p$port $ip "$get_pid_cmd"`
- test "$pid" == '' && echo "stop $ip $dest_tomcat_dir ok!"
- sleep 1
- ssh -l$user -p$port $ip "$start_cmd"
- pid=`ssh -l$user -p$port $ip "$get_pid_cmd"`
- test "$pid" != '' && echo "restart $ip $dest_tomcat_dir ok" || echo "restart $ip $dest_tomcat_dir failed!"
- done
- }
- function scp_and_unzip(){
- for ip in ${dest_ip_arr[@]};
- do
- echo "scp jar/war package to $ip $dest_lib_dir start"
- scp $src_jar $user@$ip:$dest_lib_dir
- echo "scp jar/war package to $ip $dest_lib_dir end"
- ssh -l$user -p$port $ip "$jar_xvf_cmd"
- echo "unzip $ip $dest_lib_dir package"
- done
- }
- function publish_restart(){
- scp_and_unzip
- restart
- }
- function publish_start(){
- scp_and_unzip
- start
- }
- case "$1" in
- start)
- start
- ;;
- stop)
- stop
- ;;
- status)
- status
- ;;
- restart)
- restart
- ;;
- prestart)
- publish_restart
- ;;
- pstart)
- publish_start
- ;;
- * )
- echo "usage: $0 {start|pstart|stop|status|prestart|restart}"
- exit 1
- ;;
- esac