天天看點

rhel7 mysql開機啟動_RHEL7開機自啟動shell腳本

1. RHEL7下自己建立一個腳本,如tomcat。

經過後面的幾個步驟後,這個腳本在開機的時候會執行,在這個腳本裡面可以寫你開機的時候想執行的指令,如啟動Tomcat,Oracle等服務

2. 在腳本中輸入啟動服務的指令,如(開機啟動tomcat):

# vi /etc/init.d/tomcat

#!/bin/bash

#chkconfig:  2345 08 92

#description: Start

export Java_HOME=/usr/java/jdk1.6.0_45

echo "Starting Tomcat Service ..."

/root/tomcat6.0/bin/startup.sh

注意:export JAVA_HOME=/usr/java/jdk1.6.0_45 這裡要配置環境變量,在/etc/profile中的配置在系統服務中不生效

3. 執行如下指令,将該腳本标記為可執行檔案(添加可執行的權限)

chmod +x /etc/init.d/tomcat

4. 執行如下指令将/etc/rc.d/rc.local文标記為可執行檔案

在CentOS7中,/etc/rc.d/rc.local檔案的權限被降低了,開機的時候執行在自己的腳本是不能起動一些服務的,執行下面的指令可以檔案标記為可執行的檔案

chmod +x /etc/rc.d/rc.local

5. 打開/etc/rc.d/rc.local檔案,在最後面添加如下腳本

/etc/init.d/tomcat

這樣tomcat這個腳本在開機的時候就會被執行了,以後再這裡面寫啟動服務的指令就可以了

6. 自啟動多個tomcat

需要配置各個tomcat的環境變量,在/etc/profile中的配置在系統服務中不生效,修改各自己的bin目錄catalina.sh檔案

添加如下代碼:

export JAVA_HOME=/usr/java/jdk1.8.0_144

export JRE_HOME=/usr/java/jdk1.8.0_144/jre

6.1 建立自啟動多個tomcat的shell腳本

# vi /etc/init.d/tomcat.sh 或者自己定義shell腳本位置也行(/home/sh/tomcat.sh)

#!/bin/bash

#chkconfig:  2345 08 92

#description: Start

echo "Starting Tomcat Service ..."

/root/tomcat6.0/bin/startup.sh

/root/tomcat7.0/bin/startup.sh

/root/tomcat8.0/bin/startup.sh

8.  /etc/rc.d/rc.local檔案,在最後面添加如下腳本

/home/sh/tomcat.sh

9. 如果一台伺服器上tomcat應用和資料庫在一台伺服器上,開機自啟動是需要資料庫先啟動在啟動tomcat應用,是以指令順序:啟動資料庫指令放在前面,tomcat啟動指令放在最後

# cat /etc/rc.d/rc.local

#!/bin/bash

# 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.

touch /var/lock/subsys/local

su - oracle -lc "/u01/app/oracle/product/11.2.0/dbhome_1/bin/lsnrctl start ORCL"

su - oracle -lc /u01/app/oracle/product/11.2.0/dbhome_1/bin/dbstart

/etc/init.d/starttomcat.sh

rhel7 mysql開機啟動_RHEL7開機自啟動shell腳本