Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux管理 >> Linux維護

把 DB2 與 WAS 設為開機自啟:以 init 腳本實作

要在 Linux 上讓服務開機自啟,除了 GUI 工具,也可以自己寫一份 init 腳本交給 chkconfig 管理。以下以 DB2 為例,示範整個流程。

一、切到 /etc/init.d

cd /etc/init.d

二、撰寫 DB2 腳本

vi startDB2,內容如下:

#!/bin/sh

# chkconfig: 2345 99 01

# processname:IBMDB2

# description:db2 start

DB2_HOME="/home/db2inst1/sqllib"

DB2_OWNER="db2inst1"

case "$1" in

start) su - $DB2_OWNER -c $DB2_HOME/adm/db2start; touch /var/lock/db2 ;;

stop) su - $DB2_OWNER -c $DB2_HOME/adm/db2stop; rm -f /var/lock/db2 ;;

restart|reload) $0 stop; $0 start ;;

*) echo "usage: $0 start|stop|restart|reload"; exit 1 ;;

esac

exit 0

關鍵是檔頭那三行註解:chkconfig 靠它判斷要掛在哪些 runlevel、啟動與關閉優先序,以及服務名稱與描述。缺了它,chkconfig --add 不會成功。

三、撰寫 WAS 與 TDS 腳本

vi startWasTds,依序呼叫各 profile 的啟動程式:

/opt/IBM/WebSphere/AppServer/profiles/AppSrvTds/bin/startServer.sh server1

/opt/IBM/WebSphere/AppServer/profiles/AppSrv01/bin/startServer.sh server1

/opt/IBM/ldap/V6.3/sbin/ibmslapd

檔頭同樣要有 chkconfig、processname、description 三行。

四、給權限並註冊

chmod 755 startDB2 startWasTds

chkconfig --add startDB2

chkconfig --add startWasTds

接著用 chkconfig --list 確認對應的 runlevel 為 on。

注意事項

用 755 而非 777,權限只需讀取與執行。若服務之間有先後依賴(例如 WAS 必須在 DB2 之後),要透過優先序數字或另一支總控腳本來排序,否則開機時可能因為對端還沒就緒而失敗。systemd 系統則可改寫成 .service 單元並用 systemctl enable。

Copyright © Linux教程網 All Rights Reserved