要在 Linux 上讓服務開機自啟,除了 GUI 工具,也可以自己寫一份 init 腳本交給 chkconfig 管理。以下以 DB2 為例,示範整個流程。
cd /etc/init.d
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 不會成功。
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。