Linux 安裝 Oracle 後預設不會隨系統啟動,需手動配置 dbstart/dbshut 與 systemd (或 chkconfig) 整合。本文提供經測試可用的完整腳本與步驟。
/etc/oratab 最後一欄設為 Y:orcl:/u01/app/oracle/product/11.2.0/dbhome_1:Y
$ORACLE_HOME/bin/dbstart、dbshut、lsnrctl 存在且可執行。/etc/init.d/oracle#!/bin/sh
# chkconfig: 35 80 10
# description: Oracle auto start-stop script
ORA_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
ORA_OWNER=oracle
LOG_FILE=/var/log/oracle_startup.log
[ -f $ORA_HOME/bin/dbstart ] || { echo "dbstart not found"; exit 1; }
case "$1" in
start)
echo "Starting Oracle..." | tee -a $LOG_FILE
date '+%T %a %D : Starting Oracle' >> $LOG_FILE
su - $ORA_OWNER -c "$ORA_HOME/bin/dbstart $ORA_HOME" >> $LOG_FILE 2>&1
su - $ORA_OWNER -c "$ORA_HOME/bin/lsnrctl start" >> $LOG_FILE 2>&1
touch /var/lock/subsys/oracle
echo "Done." | tee -a $LOG_FILE
;;
stop)
echo "Stopping Oracle..." | tee -a $LOG_FILE
su - $ORA_OWNER -c "$ORA_HOME/bin/lsnrctl stop" >> $LOG_FILE 2>&1
su - $ORA_OWNER -c "$ORA_HOME/bin/dbshut $ORA_HOME" >> $LOG_FILE 2>&1
rm -f /var/lock/subsys/oracle
echo "Done." | tee -a $LOG_FILE
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
;;
esac
chmod 755 /etc/init.d/oracle
chkconfig --add oracle
chkconfig --level 35 oracle on
# 關機/重啟前正常關閉
ln -sf /etc/init.d/oracle /etc/rc0.d/K01oracle
ln -sf /etc/init.d/oracle /etc/rc6.d/K01oracle
# /etc/systemd/system/oracle.service
[Unit]
Description=Oracle Database
After=network.target
[Service]
Type=forking
User=oracle
Environment=ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
Environment=ORACLE_SID=orcl
ExecStart=$ORACLE_HOME/bin/dbstart $ORACLE_HOME
ExecStop=$ORACLE_HOME/bin/dbshut $ORACLE_HOME
TimeoutSec=300
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable oracle
systemctl start oracle
# 重啟後檢查
su - oracle -c "sqlplus / as sysdba <<< 'SELECT status FROM v$instance;'"
# 應回傳 OPEN
oratab 格式錯誤(缺少冒號、SID 拼寫不一致)。dbstart 依賴 ORACLE_HOME_LISTNER 環境變數,腳本中建議顯式 export。restorecon -v /etc/init.d/oracle 或設定布林值。配置完成後,Oracle 將隨作業系統自動啟動/關閉,零人工干預。