关于在rc.local中放入Java程序无法开机自启的问题

Tanglu 系统运维 2017-10-31 5778 0

今天公司服务器因为断电重启发现了一个问题——所有部署了Tomcat应用的服务器在重启后没有自动将服务启动起来。但是rc.local文件是有可执行权限的,而且单独执行该文件中的条目可以正常启动服务,那么问题会在哪里呢?

在Tomcat启动脚本后面加上了重定向生成一个启动日志看看:

/usr/local/bin/tomcat/bin/startup.sh>>/tmp/startup.log

结果发现日志提示找不到环境变量JAVA_HOME,但是该环境变量我们明确是写进了/etc/profile中,重新source后再重启,发现服务依然没有启动,这是为什么呢?查看/etc/rc.local文件的描述信息:

# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES

# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.

# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.

# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

发现该文件是在所有服务之前运行,所以/etc/profiles或bashrc里的环境变量这个时候根本没有执行,因此环境变量自然无法生效导致服务启动报错了,解决办法就是在/etc/rc.local中加入环境变量,如图:

rc.local.png

评论