使用C和Shell實現遠程Tomcat的重啟備份腳本
下面是客戶端程序
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#define SERVPORT 6666
#define MAXDATASIZE 1024
#define SERVER_IP "10.10.10.158"
int main(int argc, char* argv[]) {
int sockfd, recvbytes;
char buf[MAXDATASIZE];
struct hostent *host;
char buffer[MAXDATASIZE];
struct sockaddr_in serv_addr;
char name[100];
printf("請輸入要操作的tomcat路徑\n");
scanf("%s",&name);
while(1){
bzero(buffer, MAXDATASIZE);
if (( sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket error!");
exit(1);
}
bzero(&serv_addr,sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(SERVPORT);
serv_addr.sin_addr.s_addr= inet_addr(SERVER_IP);
if (connect(sockfd, (struct sockaddr *)&serv_addr,sizeof(struct sockaddr)) == -1) {
perror("connect error!");
exit(1);
}
printf("請輸入要執行的命令:\n");
scanf("%s",&buf);
if(strcmp(buf,"close")==0){
close(sockfd);
return 0;
}
strcat(buffer,name);
strcat(buffer," ");
strcat(buffer,buf);
write(sockfd,buffer, sizeof(buffer));
if ((recvbytes = recv(sockfd, buf, MAXDATASIZE,0)) == -1) {
perror("recv error!");
exit(1);
}
buf[recvbytes] = '\0';
printf("客戶端返回的消息:\n %s",buf);
}
close(sockfd);
return 0;
}
下面是服務端程序
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#define SERVPORT 6666
#define BACKLOG 10
#define MAXSIZE 1024
int main() {
int sockfd,client_fd;
struct sockaddr_in my_addr;
struct sockaddr_in remote_addr;
FILE * fp;
char buffer[80];
//創建套接字
if ((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1) {
perror("socket create failed!");
exit(1);
}
//綁定端口地址
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(SERVPORT);
my_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(my_addr.sin_zero),8);
if (bind(sockfd, (struct sockaddr*)&my_addr, sizeof(struct sockaddr)) == -1) {
perror("bind error!");
exit(1);
}
if (listen(sockfd, BACKLOG) == -1) {
perror("listen error");
exit(1);
}
while (1) {
int sin_size = sizeof(struct sockaddr_in);
char shell[100] =" ";
if ((client_fd = accept(sockfd, (struct sockaddr*)&remote_addr,&sin_size)) == -1) {
perror("accept error!");
continue;
}
printf("Received a connection from %s\n", (char*)inet_ntoa(remote_addr.sin_addr));
if (!fork()) {
int rval;
char buf[MAXSIZE];
if ((rval = read(client_fd, buf, MAXSIZE)) < 0) {
perror("reading stream error!");
continue;
}
printf("客戶端發送的消息%s\n",buf);
strcat(shell,"/home/shell/tomcat_online.sh");
strcat(shell," ");
strcat(shell,buf);
fp=popen(shell,"r");
while(fgets(buf,sizeof(buf),fp) != NULL )
{
strcat(buf,"\n");
if (send(client_fd,buf,strlen(buf),0) == -1) perror("send error!");
}
pclose(fp);
close(client_fd);
exit(0);
}
close(client_fd);
}
return 0;
}
調用shell 服務器端
#!/bin/sh
Project_name=$1
Command=$2
my_Back="webapps_backup"
case "$Command" in
start)
export JAVA_HOME=/usr/local/java/jdk1.6.0_23
cd $Project_name
rm -rf work/*
$Project_name/bin/startup.sh >/dev/null
sleep 3
tompid=`ps -ef|grep $Project_name|grep -v "grep"|grep -v "$0" |awk '{print $2}'`
if ( [ -n "$tompid" ] );then
echo -e "\033[32m$Project_name啟動成功\033[0m"
else
echo -e "\033[31m$Project_name啟動失敗,請重新執行一次命令\033[0m"
fi
;;
stop)
pidlist=`ps -ef|grep $Project_name|grep -v "grep"|grep -v "$0" |awk '{print $2}'`
#echo "tomcat Id list :$pidlist"
kill -15 $pidlist
#echo "KILL $pidlist"
sleep 10
tompid=`ps -ef|grep $Project_name|grep -v "grep"|grep -v "$0" |awk '{print $2}'`
if ( [ -n "$tompid" ] && [ $tompid == $pidlist ] );then
echo -e "\033[31m$Project_name停止失敗,請重新執行一次命令\033[0m"
else
#/usr/bin/tail -1000 $Project_name/logs/catalina.out
echo -e "\033[32m$Project_name停止成功\033[0m"
fi
;;
status)
tompid=`ps -ef|grep $Project_name|grep -v "grep"|grep -v "$0" |awk '{print $2}'`
if ( [ -n "$tompid" ] );then
echo -e "\033[32m$Project_name運行中......\033[0m"
else
echo -e "\033[32m$Project_name停止中.....\033[0m"
fi
;;
restart)
export JAVA_HOME=/usr/local/java/jdk1.6.0_23
pidlist=`ps -ef|grep $Project_name|grep -v "grep"|grep -v "$0"|awk '{print $2}'`
# echo "tomcat Id list :$pidlist"
kill -15 $pidlist
#echo "KILL $pidlist:"
#echo "stop success"
#echo "start tomcat"
cd $Project_name
rm -rf work/*
cd bin
sleep 10
./startup.sh
sleep 3
/usr/bin/tail -100 $Project_name/logs/catalina.out
echo -e "\033[32m$Project_name重啟成功\033[0m"
;;
backup)
cd $Project_name
before_num=`/bin/ls webapps_backup/ |wc -l`
`/bin/find -type d -name "webapps_[0-9]*" -mtime +10 | xargs rm -fr`
#echo $my_Back
if [ ! -d "$my_Back" ]; then
`/bin/mkdir -p $my_Back`
fi
/bin/cp -frp webapps $my_Back/webapps_$(/bin/date -d "today" +"%Y%m%d_%H%M")
after_num=`/bin/ls webapps_backup/ |wc -l`
if [ $after_num -eq $(($before_num+1)) ];then
echo -e "\033[32m$Project_name備份成功\033[0m"
else
echo -e "\033[31m$Project_name備份失敗,請重新備份\033[0m"
fi
;;
rollback)
file_name=`/bin/ls -ltc $Project_name/$my_Back|sed -n 2p|awk '{print $8}'`
#echo "$Project_name/$my_Back/$file_name"
#echo $file_name
/bin/cp -frp $Project_name/$my_Back/$file_name $Project_name/$my_Back/webapps
/bin/cp -frp $Project_name/$my_Back/webapps $Project_name
rm -fr $Project_name/$my_Back/webapps
cd $Project_name
if ([ "`find ./ -maxdepth 1 -type d -cmin -1|sed -n 1p`" == './webapps' ]);then
echo -e "\033[32m$Project_name回滾成功\033[0m"
else
echo -e "\033[31m$Project_name回滾失敗,請重新執行一次\033[0m"
fi
;;
*)
echo "Usage: $0 {start|stop|restart|status|backup|rollback}"
exit 1
esac
exit 0