rc.local与环境变量的问题

来源:互联网 发布:淘宝店铺首页宽屏代码 编辑:程序博客网 时间:2024/05/22 15:41

今天又遇到了同样的问题,决定把它记下来。


当服务器上安装了tomcat服务,通常我们希望系统启动的时候能够自动将tomcat启动起来,很自然我们就会想到rc.local,于是就这样做:

echo “/usr/local/bin/tomcat/bin/startup.sh” >> /etc/rc.d/rc.local

可是事实总是屡试都爽,证明这样是行不通的,每次重启服务器都不能自动重启tomcat服务

于是我们便在/usr/local/bin/tomcat/bin/startup.sh 后面加上 >>/tmp/startup.log

跟踪日志发现找不到环境变量JAVA_HOME,这是为什么呢?


rc.local is a file, owned by root.root and should be mode 755. The rc.local file isfor initialization of programs after the system has fully booted. Thisscript is run right before login prompts are displayed. You can use thefile to run last minute startups, set certain environment variables,etc.


Conversely, if you want to start something before everything else, youwould use the regular file in /etc/rc.d called rc.sysinit, which shouldhave the same owner and permissions.


这说明rc.local运行在操作系统完全引导成功但是尚未启动login shell之前,所以我们配置在/etc/profiles或bashrc里的环境变量并未得到执行,因此在rc.local执行阶段看不到任何环境变量。


该问题的解决办法有两种:

1. 在rc.local中在startup命令之前加上export JAVA_HOME=***********

2. 可以在crontab中配置一个tomcat服务的监控脚本,1分钟探测一次tomcat是否正在运行,如果没有运行就启动一下tomcat

原创粉丝点击