本来就是个很简单的事情,不过linux接触得少,所以在这里记录一下,也可能方便到需要得人.
原因,由于公司的几个项目是部署到别人管理的服务器上,而且用的好像是虚拟平台(虚拟机), 出现过几次平台重启导致应用down掉的情况, 被客户投诉才发现这个问题, 所以使用添加开机启动项目来解决这个现象.
在 /etc/rc.local 文件中可以添加需要开机后执行的命令行.
环境 red had 5.10 64位, mysql5.1 apache2
vi /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
/usr/local/apache2/bin/apachectl start
sh /usr/tomcatGdxr/bin/startup.sh
但是在另外一台服务器上测试出问题了, tomcat启动失败. 提示什么网络不可达.
感觉是tomcat读取变量的问题,于是在tomcat/bin 目录下, 创建了一个 auto-startup.sh 文件,
touch auto-startup.sh
chmod +x auto-startup.sh
vi auto-startup.sh
export JAVA_HOME=/usr/java/jdk1.7.0_21
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:.
export PATH=$JAVA_HOME/bin:$PATH
export CATALINA_HOME=/usr/local/tomcat/
/usr/local/tomcat/bin/catalina.sh start
然后再把这个auto-startup.sh放到rc.local中,
但还是偶尔会出现项目启动时数据库连接失败的情况.
为了保险起见,于是在mysql start之后 sleep 30秒, 重启reboot了几次也成功了.
vi /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
/etc/init.d/mysqld start
sleep 30s
/usr/tomcatGdxr/bin/auto-startup.sh
sleep 30s
/usr/local/apache2/bin/apachectl start