在部署要求中,常會希望某個指令啟動的應用程式,不會因重開機而停止服務。以 Single Sign-On CAS 為例,若在 CentOS 6,可參考
這篇準備以下內容的 /etc/rc.d/init.d/<serviceName>:
- #!/bin/bash
- # chkconfig: 35 90 10
- # description: CAS server
- . /etc/init.d/functions
- SERVICE=cas
- WORKDIR=/opt/$SERVICE
- LOCK=/var/lock/subsys/$SERVICE
- start() {
- cd $WORKDIR
- /usr/bin/java -jar cas.war &
- touch $LOCK
- }
- stop() {
- killproc $SERVICE
- rm -f $LOCK
- }
- case "$1" in
- start)
- start
- ;;
- stop)
- stop
- ;;
- status)
- status $SERVICE
- ;;
- restart)
- stop
- start
- ;;
- *)
- echo "Usage: $0 {start|stop|restart|status}"
- exit 1
- esac
- exit 0
下指令載入為服務:
- chmod +x /etc/rc.d/init.d/<serviceName>
- chkconfig --add <serviceName>
- service <serviceName> start
若是 CentOS 7 環境下,似乎更簡單一些,準備 /etc/systemd/system/<serviceName>.service
- [Unit]
- Description=CAS Service
- After=network.target
- [Service]
- WorkingDirectory=/opt/cas
- "ExecStart=/usr/bin/java -jar cas.war"
- Restart=on-failure
- [Install]
- WantedBy=multi-user.target
下指令載入為服務:
- chmod +x /etc/systemd/system/<serviceName>.service
- systemctl enable <serviceName>
- systemctl start <serviceName>
沒有留言:
張貼留言