今天尝试将tomcat设置为开机启动,大家都知道只需要将启动脚本添加到/etc/rc.local下面开机就会自动执行。
/usr/local/tomcat8.0/bin/startup.sh >> /etc/rc.local
重启机器,进行测试。结果未能如愿。这是为什么呢?开始排查。手动执行没有报错。于是开始查看日志。
[root@server2 ~]# tail /var/log/boot.log Starting nginx: [ OK ] Starting crond: [ OK ] Starting atd: [ OK ] Starting certmonger: [ OK ] Using CATALINA_BASE: /usr/local/tomcat8.0 Using CATALINA_HOME: /usr/local/tomcat8.0 Using CATALINA_TMPDIR: /usr/local/tomcat8.0/temp Using JRE_HOME: /usr Using CLASSPATH: /usr/local/tomcat8.0/bin/bootstrap.jar:/usr/local/tomcat8.0/bin/tomcat-juli.jar Tomcat started.
发现日志里tomcat启动也没有异常,这下就非常疑惑了。
回想下开机顺序: /sbin/init --> /etc/inittab --> /etc/rc.d/rc.sysinit --> /etc/rc.d/* --> /etc/rc.local --> login界面(username/passwd) --> /etc/profile.d/file --> /etc/profile
rc.local 在 profile 前面执行,而jdk相关环境变量却在 profile 里。想要解决这个问题就需要在 tomcat脚本启动前就执行/etc/profile 才行。
[root@server2 ~]# vim /etc/rc.local #!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don't # want to do the full Sys V style init stuff. touch /var/lock/subsys/local source /etc/profile # 执行下 /etc/profile /usr/local/tomcat8.0/bin/startup.sh
echo "tomcat test."
再次重启机器测试。
[root@server2 ~]# tail /var/log/boot.log Starting crond: [ OK ] Starting atd: [ OK ] Starting certmonger: [ OK ] Using CATALINA_BASE: /usr/local/tomcat8.0 Using CATALINA_HOME: /usr/local/tomcat8.0 Using CATALINA_TMPDIR: /usr/local/tomcat8.0/temp Using JRE_HOME: /usr/local/jdk1.8 Using CLASSPATH: /usr/local/tomcat8.0/bin/bootstrap.jar:/usr/local/tomcat8.0/bin/tomcat-juli.jar Tomcat started. tomcat test.
日志OK。
[root@server2 ~]# netstat -ntplu | grep 8080 tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 1590/java
服务也OK.
这样的问题在以后可能也会遇到。可见,基础的原理和知识对解决问题有多重要。Linux开机执行文件的顺序一定要牢记。