参考自(亲自试验,并稍作修改):
http://www.oracle-base.com/articles/linux/automating-database-startup-and-shutdown-on-linux.php
support.filecatalyst.com/index.php?/Knowledgebase/Article/View/210/0/starting-tomcat-as-a-linux-service
tomcat加入服务并设置自启动在另一篇:http://blog.csdn.net/zxnlmj/article/details/22933477
oracle数据库安装见:http://blog.csdn.net/zxnlmj/article/details/22826149
1、编辑/etc/oradb文件,将每个数据库实例的重启标志由N都设置为Y:
su root gedit /etc/oratab
# The first and second fields are the system identifier and home # directory of the database respectively. The third filed indicates # to the dbstart utility that the database should , "Y", or should not, # "N", be brought up at system boot time. # # Multiple entries with the same $ORACLE_SID are not allowed. # # sunlight:/home/oracle/product/11.2/db_1:Y
2、新建/etc/init.d/oracle文件,并添加如下配置:
su root: gedit /etc/init.d/oracle
添加如下内容:
#!/bin/sh # chkconfig: 345 99 10 # description: Oracle auto start-stop script. # # Set ORA_OWNER to the user id of the owner of the # Oracle database software. # ORA_OWNER为在linux中安装oracle数据库时建立的属于dba组的用户名oracle,而非登陆oracle数据库时的用户名 ORA_OWNER=oracle case "$1" in 'start') # Start the Oracle databases: # The following command assumes that the oracle login # will not prompt the user for any values # Remove "&" if you don't want startup as a background process. su $ORA_OWNER -c "/home/oracle/scripts/startup.sh >> /home/oracle/scripts/startup_shutdown.log 2>&1" & touch /var/lock/subsys/dbora ;; 'stop') # Stop the Oracle databases: # The following command assumes that the oracle login # will not prompt the user for any values su $ORA_OWNER -c "/home/oracle/scripts/shutdown.sh >> /home/oracle/scripts/startup_shutdown.log 2>&1" rm -f /var/lock/subsys/dbora ;; esac
su root [root@JTWF Desktop]# chmod 750 /etc/init.d/oracle
[root@JTWF Desktop]# chkconfig --add oracle [root@JTWF Desktop]#
# mkdir -p /home/oracle/scripts # chown oracle.oinstall /home/oracle/scripts [root@JTWF Desktop]# chown oracle.dba /home/oracle/scripts
#!/bin/bash export TMP=/tmp export TMPDIR=$TMP export PATH=/usr/sbin:/usr/local/bin:$PATH export ORACLE_HOSTNAME=JTWF #export ORACLE_UNQNAME=db12c export ORACLE_SID=sunlight ORAENV_ASK=NO . oraenv ORAENV_ASK=YES # Start Listener lsnrctl start # Start Database sqlplus / as sysdba << EOF STARTUP; EXIT; EOF
#!/bin/bash export TMP=/tmp export TMPDIR=$TMP export PATH=/usr/sbin:/usr/local/bin:$PATH export ORACLE_HOSTNAME=JTWF #export ORACLE_UNQNAME=db12c export ORACLE_SID=sunlight ORAENV_ASK=NO . oraenv ORAENV_ASK=YES # Stop Database sqlplus / as sysdba << EOF SHUTDOWN IMMEDIATE; EXIT; EOF
(2)、ORACLE_HOSTNAME在默认安装完Oracle数据库后,默认为localhost,最好能修改ip地址到机器名的映射,具体见:http://blog.csdn.net/zxnlmj/article/details/23097387
(3)、ORACLE_SID在Oracle数据库时配置环境变量时已配置。
6、修改上述两个脚本的权限及所有者:
[root@JTWF admin]# chmod u+x /home/oracle/scripts/startup.sh /home/oracle/scripts/shutdown.sh [root@JTWF admin]# chown oracle.dba /home/oracle/scripts/startup.sh /home/oracle/scripts/shutdown.sh
7、测试使用服务启动、关闭Oracle
su root # service oracle start # service oracle stop说明:(1)、安装完Oracle数据库软件、并创建数据库后,oracle数据库默认已经启动,测试时可先stop,再start
(2)、启动日志在/etc/init.d/oracle文件中已有配置:/home/oracle/scripts/startup_shutdown.log
(3)、service oracle中的oracle和/etc/init.d/oracle及及chkconfig --add oracle三者是一致的。
(4)、注意:若是没执行6步骤,在在/home/oracle/scripts/startup_shutdown.log日志里会报如下错误:
bash: /home/oracle/scripts/startup.sh: Permission denied bash: /home/oracle/scripts/startup.sh: Permission denied
另外可以执行reboot命令,等待机器重启后,使用sqlplus命令连接oracle即可判断oracle是否自动重启
su root reboot
重启后:
source /home/oracle/.bash_profile // 若是sqlplus命令提示不存在,则需手动使环境变量生效,若提示,则该命令不必执行。 sqlplus its/oracle@orcl // 能连接进入oracle数据库则说明设置成功
8、设置linux系统重启时,oracle先自启动,tomcat后自启动:
思考方案:
1、在oracle启动脚本里,执行启动tomcat的命令(tomcat不必设置自启动),但是oracle脚本里已经su oracle,而tomcat是root用户安装的,无执行权限;将tomcat安装目录改为oracle用户所有,则感觉不妥,弃之。 2、在tomcat的自启动脚本里添加循环,查看oracle进程是否存在,存在则启动tomcat,一定时间内仍然没有oracle进程,则循环结束,亦不启动tomcat;此种感觉麻烦,且对shell脚本不熟悉,弃之。 3、设置oracle与tomcat自启动脚本里的启动与停止的优先级,可行。
8.1、对/etc/init.d/oracle与/etc/init.d/tomcat文件的运行级说明:
在http://blog.csdn.net/zxnlmj/article/details/22933477中的tomcat配置文件/etc/init.d/tomcat中有这么两行注释chkconfig、description如下:
#!/bin/bash # This is the init script for starting up the # Jakarta Tomcat server # # chkconfig: 345 91 10 # description: Starts and stops the Tomcat daemon. #
oracle运行级文件的配置如下:
#!/bin/sh # chkconfig: 345 99 10 # description: Oracle auto start-stop script.
若是tomcat的运行级文件无这两行注释,在执行chkconfig --add tomcat据说会报错。
其中chkconfig一行说明tomcat在系统运行级别为345时自启动,启动优先权为91,停止优先权为10,优先权越小,表示越早启动,越早关闭。
oracle运行级文件类似。
对linux系统服务的执行等级说明如下:
--level<等级代号> 指定读系统服务要在哪一个执行等级中开启或关毕。 等级0表示:表示关机 等级1表示:单用户模式 等级2表示:无网络连接的多用户命令行模式 等级3表示:有网络连接的多用户命令行模式 等级4表示:不可用 等级5表示:带图形界面的多用户模式 等级6表示:重新启动
既然知道了运行优先权,则可设置oracle与tomca启动停止优先权,让系统启动时,oracle先启动,tomcat后启动,系统关闭时,tomcat先关闭,oracle后关闭。
su root gedit /etc/init.d/oracle将chkconfig一行改为:
# chkconfig: 345 98 11
修改tomcat运行级文件:
su root gedit /etc/init.d/tomcat
将chkconfig一行改为:
# chkconfig: 345 99 10
su root reboot
重启后,访问tomcat服务即可,看能否访问(前提是tomcat应用中连接oracle数据库了)。
当然,要是tomcat部署的应用中没有连接数据库,也不必如此配置了。
over!
#!/bin/sh # chkconfig: 345 99 10 # description: Oracle auto start-stop script. # # Set ORA_OWNER to the user id of the owner of the # Oracle database software. # ORA_OWNER=oracle start(){ # Start the Oracle databases: # The following command assumes that the oracle login # will not prompt the user for any values # Remove "&" if you don't want startup as a background process. echo "oracle is starting in daemon" su - $ORA_OWNER -c "/cm/oracle/scripts/startup.sh >> /cm/oracle/scripts/startup_shutdown.log 2>&1" & echo "oracle is started" touch /var/lock/subsys/dbora } stop(){ # Stop the Oracle databases: # The following command assumes that the oracle login # will not prompt the user for any values echo "oracle is stopping" su $ORA_OWNER -c "/cm/oracle/scripts/shutdown.sh >> /cm/oracle/scripts/startup_shutdown.log 2>&1" rm -f /var/lock/subsys/dbora echo "oracle is stopped" } restart(){ stop start } status(){ numproc_listener=`ps -ef | grep tnslsnr | grep -v "grep tnslsnr" | wc -l` if [ $numproc_listener -gt 0 ]; then echo "Oracle's listener is running!" else echo "Oracle's listener is stopped!" fi numproc_oraInstance=`ps -ef | grep ora_ | grep -v "grep ora_" | wc -l` if [ $numproc_oraInstance -gt 0 ]; then echo "Oracle's instance is running!" else echo "Oracle's instance is stopped!" fi } case "$1" in 'start') start ;; 'stop') stop ;; 'restart') restart ;; 'status') status ;; esac