Ubuntu
創建腳本以啟動/停止/重新啟動服務的好習慣?
我想知道創建一個好的腳本來啟動/停止/重新啟動某些服務的好習慣是什麼。我會盡量讓自己更清楚,好嗎?現在,我做了這樣的事情:假設我想為
start/stop/restart
服務創建一個腳本,所以我創建了一個文件夾/company/service name/
,然後放了start.sh
和stop.sh
,它們是這樣的:start.sh
#!/bin/bash #VARIABLES SERVICE_NAME="<service name>" USERDEPLOYER="<service name>_deployer" FOLDER=/company/<service name>/ KEYWORD="<keyword>" # #CHECKING SYSTEM STATUS PROC=`ps -ef | grep $SERVICE_NAME | grep $KEYWORD | grep -v grep | awk -F" " '{ print $2 }'`; if [ $PROC ]; then echo "$SERVICE_NAME is running!" echo "Stop then first!" exit fi ### # #STARTING if [[ `/usr/bin/whoami` == $USERDEPLOYER ]] then pushd . echo " " echo "Starting $SERVICE_NAME..." echo "cd $FOLDER" cd $FOLDER #COMMAND <command to start the service> & sleep 20 PROC=`ps -ef | grep $SERVICE_NAME | grep $KEYWORD | grep -v grep | awk -F" " '{ print $2 }'`; if [ -n "$PROC" ] && [ "$PROC" != "" ] then echo "OK: system started." else echo "ERROR: system process not found!" fi echo "script execution finished!" popd else echo "User must be $USERDEPLOYER !" fi
stop.sh
#!/bin/bash #VARIABLES SERVICE_NAME="<service name>" USERDEPLOYER="<service name>_deployer" KEYWORD="python" if [[ `/usr/bin/whoami` == $USERDEPLOYER ]] then pushd . echo "Stopping $SERVICE_NAME......" #KILLING PROCESS processPID=`ps -ef | grep $SERVICE_NAME | grep $KEYWORD | grep -v grep | awk -F" " '{ print $2 }'` echo "Trying to kill process with key $SERVICE_NAME - ignore error messages below." kill $processPID sleep 10 while [ -n "$processPID" ] do echo "Waiting process ($processPID) to shutdown...20s" sleep 20 processPID=`ps -ef | grep $SERVICE_NAME | grep $KEYWORD | grep -v grep | awk -F" " '{ print $2 }'` done echo "Ensured process with key $SERVICE_NAME is no longer running." popd else echo "User must be $USERDEPLOYER !" fi
之後,我創建了一個使用者
service name_deployer
,然後授予此文件夾和這些腳本的所有權,start.sh
並stop.sh
授予權限read, write and execute
。
/etc/init.d/
然後在as中創建以下腳本service name-service
:#!/bin/bash # # Linux chkconfig stuff: # # chkconfig: 2345 56 10 # 2345 56 # 2345 10 # description: <description> # Source function library. SERVICE_NAME="<service name>-service" SERVICE_USER="<service name>_deployer" FOLDER="/company/<service name>/" start() { if [[ `/usr/bin/whoami` == $SERVICE_USER ]] then cd $FOLDER ./start.sh #NOT USER _root else cd $FOLDER su $SERVICE_USER ./start.sh fi } stop() { cd $FOLDER su $SERVICE_USER ./stop.sh } #Body main case "$1" in start) start ;; stop) stop ;; restart) echo "Restarting $SERVICE_NAME..." echo " " stop sleep 10 start ;; *) echo $"Usage: $0 {start|stop|restart}" exit 1 esac exit 0
鑑於所有權
service name_deployer
和許可read, write and execute.
然後將服務添加到服務列表中,如下所示:/sbin/chkconfig –add
service name-service
(suse 和其他)或 update-rc.dservice name-service
預設值(ubuntu)就這樣!你們認為這是一個好方法嗎?我只是問,因為我想為這種腳本和程序創建一個好的標準。對不起,如果你們認為這是一個蹩腳的問題,但對我來說,這種程序非常重要。
謝謝你們!
而是使用sudo來管理使用者訪問。
/etc/init.d/
按照通常的約定創建一個初始化腳本。(Ubuntu/Debian 的腳本應該start-stop-daemon
用於啟動、重試停止和檢查程序狀態。)然後執行visudo
並添加適當的條目以允許使用者管理此服務。例如:User_Alias SERVICE_USERS = bob, jane Cmnd_Alias SERVICE_CMNDS = service service-name SERVICE_USERS ALL = SERVICE_CMNDS
然後指定的使用者可以執行
sudo service service-name start
等等。