linux shell的创建与启动
1.创建shell脚本,输入linux命令:
touch my.sh
2.编辑shell脚本,输入linux命令:
vi my.sh
3.在shell脚本进行编辑:顺便记一次Jenkins的自动启动的命令
#!/bin/bash #export BUILD_ID=dontKillMe这一句很重要,这样指定了,项目启动之后才不会被Jenkins杀掉。 export BUILD_ID=dontKillMe #Jenkins中编译好的jar名称 jar_name=platform-1.0.0.RELEASE.jar #复制jar包到/usr/local/kidcloud/yml下的admin cp ${jar_name} /usr/local/kidcloud/yml/platform #进入admin下 cd /usr/local/kidcloud/yml/platform #把yml文件压缩到jar包中 jar -uvf ${jar_name} BOOT-INF/classes/application.yml jar -uvf ${jar_name} BOOT-INF/classes/application.properties #把jar包复制到 /usr/local/kidcloud下 cp ${jar_name} /usr/local/kidcloud #进入kidcloud下 cd /usr/local/kidcloud #获取运行编译好的进程ID,便于我们在重新部署项目的时候先杀掉以前的进程 pid=$(cat /usr/local/kidcloud/codespace/platform.pid) #杀掉以前可能启动的项目进程 kill -9 ${pid} #启动jar,指定SpringBoot的profiles为test,后台启动 nohup java -jar -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/usr/local/kidcloud/heapdump_platform.hprof -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -Xms1024m -Xmx1024m -Xmn256m -Xss256k -XX:SurvivorRatio=8 -XX:+UseConcMarkSweepGC -Dspring.profiles.active=test ${jar_name} & #将进程ID存入到rabbitmq.pid文件中 echo $! > /usr/local/kidcloud/codespace/platform.pid
4.对shell进行授权,输入linux命令:
chmod +x my.sh
5.执行,输入linux命令:
./my.sh
6.实现java项目运维启动的脚本:
#!/bin/bash export BUILD_ID=dontKillMe function conFun(){ echo -n "继续请输入yes/y,停止则输入其它:" read arg1 case $arg1 in Y|y|YES|yes) ;; N|n|NO|no) exit ;; *) exit ;; esac } base_path=/test/prd release_path=${base_path}/backup/toRelease jar_name=test.jar port=8081 replace_port=8181 a_xmx=1536 b_xmx=1537 run_xmx=${a_xmx} kill_xmx=${b_xmx} run_path=${base_path}/01 prevPid= echo -n "请输入1或2,1代表启动端口1,2代表启动端口2:" read arg case $arg in 1) port=8081 replace_port=8181 run_xmx=${a_xmx} kill_xmx=${b_xmx} run_path=${base_path}/01 prevPid="`ps -ef | grep "java -jar -Xmx${kill_xmx}m ${jar_name}" | grep -v grep |awk '{print $2}' `" echo "项目运行的pid:$prevPid" if [ -z "$prevPid" ] then echo '端口2的项目未启动,启动失败' conFun fi ;; 2) port=8181 replace_port=8081 run_xmx=${b_xmx} kill_xmx=${a_xmx} run_path=${base_path}/02 prevPid="`ps -ef | grep "java -jar -Xmx${kill_xmx}m ${jar_name}" | grep -v grep |awk '{print $2}' `" echo "项目运行的pid:$prevPid" if [ -z "$prevPid" ] then echo '端口1的项目未启动,启动失败' conFun fi ;; *) echo '输入错误,即将退出' exit ;; esac echo "新的端口:$port,要替换的端口:$replace_port, 要执行的新的xmx:$run_xmx, 要杀掉的xmx:$kill_xmx" #拉出配置文件 cd ${release_path} jar -xvf ${jar_name} BOOT-INF/classes/application-prd.yml #修改prd.yml配置文件的port sed -i "5s/$replace_port/$port/g" BOOT-INF/classes/application-prd.yml #yml文件放回去 jar -uvf ${jar_name} BOOT-INF/classes/application-prd.yml #复制到run_path中 cp -r ${jar_name} ${run_path} cd ${run_path} #启动新项目,先杀一下项目 prevPidKill_new="`ps -ef | grep "java -jar -Xmx${run_xmx}m ${jar_name}" | grep -v grep |awk '{print $2}' `" #[ $prevPidKill_new ]&&echo ${prevPidKill_new}&&kill ${prevPidKill_new} echo "杀掉要运行的项目的进程:$prevPidKill_new" #nohup java -jar -Xmx${run_xmx}m ${jar_name} >/dev/null 2>&1 & #等待20秒 sleep 20s #查询项目是否启动起来 #没起来,终止 prevPidKill_new_start="`ps -ef | grep "java -jar -Xmx${run_xmx}m ${jar_name}" | grep -v grep |awk '{print $2}' `" if [ -z "$prevPidKill_new_start" ] then echo '项目启动失败' conFun fi #起来了,修改nginx proxy_pass http://127.0.0.1:8081/mgt; #sed -i "s/127.0.0.1:$replace_port/127.0.0.1:$port/g" /etc/nginx/nginx.conf sed -i "s/127.0.0.1:$replace_port/127.0.0.1:$port/g" /test/prd/nginx.conf #启动nginx nginx_result="`nginx -s reload`" echo "nginx_result: ${nginx_result}" #杀掉旧项目 oldPidKill="`ps -ef | grep "java -jar -Xmx${kill_xmx}m ${jar_name}" | grep -v grep |awk '{print $2}' `" #[ $oldPidKill ]&&echo ${oldPidKill}&&kill ${oldPidKill} echo '杀掉另一个项目的进程'