萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Linux 教學

Linux 服務開機自啟標準化實踐:以 DB2、WebSphere、TDS 為例

將第三方服務(DB2、WebSphere、IBM TDS)納入 Linux 開機自啟,需編寫符合 LSB 標準的 init 腳本並註冊 chkconfig。本文提供可直接落地的腳本模板與註冊流程。

標準 init 腳本模板結構

#!/bin/sh
# chkconfig: 2345 99 01
# processname: IBM_DB2
# description: db2 auto start

SERVICE_HOME="/home/db2inst1/sqllib"
SERVICE_OWNER="db2inst1"

case "$1" in
  start)
    echo -n "Starting IBM DB2: "
    su - $SERVICE_OWNER -c "$SERVICE_HOME/adm/db2start"
    touch /var/lock/subsys/db2
    echo "OK"
    ;;
  stop)
    echo -n "Stopping IBM DB2: "
    su - $SERVICE_OWNER -c "$SERVICE_HOME/adm/db2stop"
    rm -f /var/lock/subsys/db2
    echo "OK"
    ;;
  restart|reload)
    $0 stop
    $0 start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|reload}"
    exit 1
    ;;
esac
exit 0

關鍵標註說明(chkconfig 必讀)

  • # chkconfig: 2345 99 01:Runlevel 2/3/4/5 啟動,啟動優先級 99(較晚),停止優先級 01(較早)。
  • # processname: 進程標識名。
  • # description: 服務描述。
  • 鎖文件 /var/lock/subsys/<name> 供 service 狀態檢查使用。
  • 以服務擁有者身份執行啟停命令(su - user -c cmd)。

WebSphere + TDS 綜合啟動腳本範例

#!/bin/sh
# chkconfig: 2345 99 01
# processname: IBM_WAS_TDS
# description: filenet was tds

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

# TDS (IBM Directory Server)
/opt/ibm/ldap/V6.3/sbin/ibmdiradm -l dsrdbm01 -t
/opt/ibm/ldap/V6.3/sbin/ibmslapd

註冊生效三步驟

chmod 755 /etc/init.d/startDB2
chmod 755 /etc/init.d/startWasTds
chkconfig --add startDB2
chkconfig --add startWasTds
chkconfig --list | grep -E 'startDB2|startWasTds'  # 驗證輸出 2345:on

systemd 時代遷移提示

CentOS 7+/RHEL 7+ 建議改寫為 .service Unit 文件放入 /etc/systemd/system/,使用 systemctl enable 管理,但 SysV init 腳本兼容模式仍支援上述做法。

標準化腳本 + chkconfig 註冊,讓任何第三方服務擁有原生級開機自啟體驗。

copyright © 萬盛學電腦網 all rights reserved