前情提要
默认小伙伴都是拥有一台云服务器,和一个域名哈[aru_41]
开始教程
第一步
启动 jar
包脚本
RESOURCE_NAME=test1.jar tpid=`ps -ef|grep $RESOURCE_NAME|grep -v grep|grep -v kill|awk '{print $2}'` if [ ${tpid} ]; then echo 'Stop Process...' kill -15 $tpid fi sleep 5 tpid=`ps -ef|grep $RESOURCE_NAME|grep -v grep|grep -v kill|awk '{print $2}'` if [ ${tpid} ]; then echo 'Kill Process!' kill -9 $tpid else echo 'Stop Success!' fi tpid=`ps -ef|grep $RESOURCE_NAME|grep -v grep|grep -v kill|awk '{print $2}'` if [ ${tpid} ]; then echo 'App is running.' else echo 'App is NOT running.' fi rm -f tpid nohup java -jar /dbc/dbc/dbc/dbc/dbc/jar/$RESOURCE_NAME & echo $! > tpid echo Start Success!
第二步
默认你已经有了Nginx了
说个题外话,如果你不知道什么是Nginx估计你看这篇文章也会非常的懵逼,本篇文章不解释相关技术,只注重实现,相关技术自行百度[aru_34]
1.配置一个域名,无论你是子域名还是主域名都可以
2.编写Nginx配置文件
upstream lbs2 { server dbc655.top:8081 ; server dbc655.top:8082 backup; }
server { listen 80; server_name test.dbc655.top; location / { proxy_pass http: //lbs2; proxy_redirect default; #存放用户的真实ip proxy_set_header Host $host; proxy_set_header X - Real - IP $remote_addr; proxy_set_header X - Forwarded - For $proxy_add_x_forwarded_for; proxy_next_upstream error timeout http_503 non_idempotent; #开启错误拦截配置, 一定要开启proxy_intercept_errors on; }#遇见错误码 < 404 500 502 503 504 > , 统一转化为200,并且转发到 / default_api这里,由这里返回json数据error_page 404 500 502 503 504 = 200 / default_api; location = /default_api { default_type application/json; return 200 '{"code":"-1","msg":"invoke fail, not found "}'; } }
第三步
这里我们可以使用宝塔的计划任务帮助我们实现
第四步
编写对应的监控脚本
#!/bin/bash dir1=/dbc/dbc/dbc/dbc/xxx dir2=/dbc/dbc/dbc/dbc/xxx dir3=/dbc/dbc/dbc/dbc/xxx dir4=/dbc/dbc/dbc/dbc/xxx #指定需要监视的文件夹 rsync_file=/test.sh #发现文件有变化时需要执行的脚本文件 while inotifywait -r $dir1 $dir2 $dir3 $dir4 -e modify,delete,create # do bash $rsync_file #执行同步文件的脚本 done
汇总
完整脚本如下
cd /dbc/dbc/dbc/dbc/dbc git log -1 --pretty=format:'%an' rm -f /dbc/dbc/dbc/dbc/dbc/dbc-http/src/main/resources/application-dev.properties OUTPUT="$(git log -1 --pretty=format:'%an')" OUTPUT2=" 将代码送到【test128】" OUTPUT3=${OUTPUT}${OUTPUT2} sendemail -f dbc@qq.com -t "dbc@qq.com" -s smtp.qq.com:587 -u "${OUTPUT3}" -o tls=yes -o message-content-type=text -o message-charset=utf8 -xu dbc@qq.com -xp uodrerisbemcbfde -m "DBC的无感自动发包即将启动!" mvn clean sendemail -f dbc@qq.com -t "dbc@qq.com" -s smtp.qq.com:587 -u '【test128】主服务正在构建' -o tls=yes -o message-content-type=text -o message-charset=utf8 -xu dbc@qq.com -xp uodrerisbemcbfde -m "【test128】主服务无感构建中!" mvn install cd /dbc/dbc/dbc/dbc/dbc/jar mv dbc-test1.jar RESOURCE_NAME=dbc-test1.jar tpid=`ps -ef|grep $RESOURCE_NAME|grep -v grep|grep -v kill|awk '{print $2}'` if [ ${tpid} ]; then echo 'Stop Process...' kill -15 $tpid fi sleep 5 tpid=`ps -ef|grep $RESOURCE_NAME|grep -v grep|grep -v kill|awk '{print $2}'` if [ ${tpid} ]; then echo 'Kill Process!' kill -9 $tpid else echo 'Stop Success!' fi tpid=`ps -ef|grep $RESOURCE_NAME|grep -v grep|grep -v kill|awk '{print $2}'` if [ ${tpid} ]; then echo 'App is running.' else echo 'App is NOT running.' fi rm -f tpid nohup java -jar dbc/jar/$RESOURCE_NAME & echo $! > tpid echo Start Success! sendemail -f dbc@qq.com -t "dbc@qq.com" -s smtp.qq.com:587 -u '主服务【test128】无感构建完成' -o tls=yes -o message-content-type=text -o message-charset=utf8 -xu dbc@qq.com -xp uodrerisbemcbfde -m "主服务正在启动中!备用服务正在准备构建!" cd dbc/test2/dbc git pull \cp dbc/application-dev.properties dbc/resources mvn clean mvn install cd dbc/test2/dbc/jar mv dbc.jar dbc-test2.jar RESOURCE_NAME=dbc-test2.jar tpid=`ps -ef|grep $RESOURCE_NAME|grep -v grep|grep -v kill|awk '{print $2}'` if [ ${tpid} ]; then echo 'Stop Process...' kill -15 $tpid fi sleep 5 tpid=`ps -ef|grep $RESOURCE_NAME|grep -v grep|grep -v kill|awk '{print $2}'` if [ ${tpid} ]; then echo 'Kill Process!' kill -9 $tpid else echo 'Stop Success!' fi tpid=`ps -ef|grep $RESOURCE_NAME|grep -v grep|grep -v kill|awk '{print $2}'` if [ ${tpid} ]; then echo 'App is running.' else echo 'App is NOT running.' fi rm -f tpid nohup java -jar dbc/jar/$RESOURCE_NAME & echo $! > tpid echo Start Success! sleep 30 OUTPUT4="$(tail -5 dbc/jar/logs/poverty-prevention.log)" OUTPUT5=" 详细信息请移步DBC宝塔!\n" OUTPUT6=${OUTPUT5}${OUTPUT4} sendemail -f dbc@qq.com -t "dbc@qq.com" -s smtp.qq.com:587 -u "DBC无感发包脚本已经全部完成" -o tls=yes -o message-content-type=text -o message-charset=utf8 -xu dbc@qq.com -xp uodrerisbemcbfde -m "${OUTPUT6}"
大功告成![aru_50]
常见问题
监控突然无效了
本文作者为DBC,转载请注明。