1. 背景
jenkins发布时,使用ansible执行远程主机上的启动tomcat脚本发现不生效,启动tomcat的脚本中有环境变量。
ansible主机为:172.16.35.8
tomcat服务器为:172.16.35.56
2. tomcat启动脚本
1[app@cecntos jenkins]$ cat tomcat_start.sh2#!/bin/bash34export CATALINA_YCLOANS_BASE CATALINA_YCLOANS_HOME TOMCAT_YCLOANS_HOME
5export JAVA_HOME=/usr/local/java/jdk1.6.0_45
6export PATH=$JAVA_HOME/bin:$PATH
7export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar8
9
10#1.启动tomcat
11echo"setup01:启动tomcat"12cd/usr/local/tomcat/bin/ && ./startup.sh
13Process=`ps -ef |grep '/usr/local/tomcat' |grep -v grep|awk '{print $2}'|wc -l`
14sleep1015if [ "$Process" -eq "0"];then16 echo "启动tomcat失败!"
17exit
18else
19 echo "启动tomcat完成!"20fi
3. 现象
在tomcat服务器执行脚本无问题,可以正常启动。
启动
1[app@cecntos jenkins]$ ps -ef|grep tomcat
2app1075 10257 0 11:26 pts/4 00:00:00grep tomcat3[app@cecntos jenkins]$4[app@cecntos jenkins]$ sh tomcat_start.sh
5setup01:启动tomcat
6Using CATALINA_CMIS_BASE:/usr/local/tomcat
7Using CATALINA_CMIS_HOME:/usr/local/tomcat
8Using CATALINA_TMPDIR:/usr/local/tomcat/temp
9Using JRE_HOME:/usr/local/java/jdk1.6.0_45/jre
10Using CLASSPATH:/usr/local/tomcat/bin/bootstrap.jar: /usr/local/tomcat/bin/tomcat-juli.jar
11Tomcat started.12 启动tomcat完成!
在tomcat服务器查看状态正常;
1[app@cecntos jenkins]$ ps -ef|grep tomcat
2app1181 1178 75 11:26 pts/4 00:00:18 /usr/local/java/jdk1.6.0_45/jre/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.rmi.server.hostname=172.16.136.xx -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=8999 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Xms512m -Xmx2048m -XX:PermSize=1024M -XX:MaxNewSize=512m -XX:MaxPermSize=512m -XX:ParallelGCThreads=50 -XX:+UseConcMarkSweepGC -Xloggc:./gc.log -XX:+UseConcMarkSweepGC -Xloggc:./gc.log -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./dump.core -Djava.endorsed.dirs=/endorsed -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
3app1610 10257 0 11:27 pts/4 00:00:00 grep tomcat
在tomcat服务器停止后去ansible主机执行启动脚本;
1[app@cecntos jenkins]$ ps -ef |grep 'tomcat' |grep -v grep|awk '{print $2}'|xargs kill -9
2[app@cecntos jenkins]$ ps -ef|grep tomcat
3app15110 28067 0 13:56 pts/4 00:00:00 grep tomcat
在ansible主机执行tomcat启动脚本;
1[jenkins@VMcentos ~]$ sudo ansible 172.16.35.56 -u app -m shell -a 'sh /home/app/jenkins/tomcat_start.sh'
2172.16.35.56 | SUCCESS | rc=0 >>3setup01:启动tomcat
4Tomcat started.5 启动tomcat完成!
显示启动成功,但是查看却没有进程;
1[app@cecntos jenkins]$ ps -ef|grep tomcat
2app2390 10257 0 11:28 pts/4 00:00:00grep tomcat3[app@cecntos jenkins]$
4. 解决方法
在ansible执行启动脚本时,加一个“nohup”参数就可以解决
sh "sudo ansible ${serverIP} -u ${serverName} -m shell -a 'nohup sh ${cmisonlineShellName}'"
1[jenkins@VM_131_4_centos ~]$ sudo ansible 172.16.35.56 -u app -m shell -a 'nohup sh /home/app/jenkins/tomcat_start.sh'
2172.16.35.56 | SUCCESS | rc=0 >>3setup01:启动tomcat
4Tomcat started.5 启动tomcat完成!
查看;
1[app@cecntos jenkins]$ ps -ef|grep tomcat
2app1181 1178 75 11:26 pts/4 00:00:18 /usr/local/java/jdk1.6.0_45/jre/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.rmi.server.hostname=172.16.136.xx -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=8999 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Xms512m -Xmx2048m -XX:PermSize=1024M -XX:MaxNewSize=512m -XX:MaxPermSize=512m -XX:ParallelGCThreads=50 -XX:+UseConcMarkSweepGC -Xloggc:./gc.log -XX:+UseConcMarkSweepGC -Xloggc:./gc.log -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./dump.core -Djava.endorsed.dirs=/endorsed -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
3app1610 10257 0 11:27 pts/4 00:00:00 grep tomcat
5. 原因
ansible执命令,默认不加载环境变量,nohup参数让脚本在后台执行,就可以加载里面的环境变量。