簡介
通常一個獨立的服務,程式,中介軟體成後,為便於測試,上線部署都會編寫一一個shell用於對程式的啟動,重啟 ,檢視狀態,停止,解除安裝 等動作, 以下是 一個使用shell指令碼來實現的簡單demo, 直接見程式碼。
功能
- 啟動: start
- 重啟: restart
- 停止:stop
- 解除安裝:uninstall
- 檢視執行狀態: status
- 獲取程式版本號:version
程式碼
將程式碼中的 $YOUR_APP_NAME
替換成真實的程式檔名, shell檔名稱: manager.sh
#!/bin/bash
mode=$1 # uninstall/start/restart/stop/status/version
###############################################################################
function echo_red() {
echo -e "\033[1;31m$1\033[0m"
}
function echo_green() {
echo -e "\033[1;32m$1\033[0m"
}
function echo_yellow() {
echo -e "\033[1;33m$1\033[0m"
}
function echo_done() {
sleep 0.5
echo "$(gettext 'complete')"
}
function echo_failed() {
echo_red "$(gettext 'fail')"
}
function log_success() {
echo_green "[+] $1"
}
function log_error() {
echo_red "[x] $1"
}
install() {
log_success "deploy the program ..."
nohup ./$YOUR_APP_NAME -c APP_CONFIG.yml >/dev/null 2>&1 &
sleep 10
n=$(ps aux | grep -v grep | grep $YOUR_APP_NAME -c)
if [ $n -eq 0 ]; then
log_error "no $YOUR_APP_NAME is running ..."
return 0
fi
log_success "install done!"
}
stop() {
n=$(ps aux | grep -v grep | grep $YOUR_APP_NAME -c)
if [ $n -eq 0 ]; then
return
fi
log_success "stop YOUR_APP_NAME ..."
ps aux | grep '$YOUR_APP_NAME' | grep -v grep | awk '{print $1}' | xargs kill -9
sleep 5
n=$(ps aux | grep -v grep | grep $YOUR_APP_NAME -c)
if [ $n -eq 0 ]; then
log_success "no $YOUR_APP_NAME is running ..."
fi
return 0
}
status() {
n=$(ps aux | grep -v grep | grep $YOUR_APP_NAME -c)
if [ $n -eq 0 ]; then
log_success "no $YOUR_APP_NAME is running ..."
else
log_success "$YOUR_APP_NAME is running ..."
fi
return 0
}
uninstall_service() {
stop
# 清理邏輯,刪除程式生成的檔案
rm -f ./$YOUR_APP_NAME.db
rm -rf ./log
log_success "uninstall done!"
}
restart_service() {
stop
install
log_success "restart done!"
}
// 獲取程式版本號
version (){
./$YOUR_APP_NAME --version
}
# usage,使用說明書
function usage() {
echo gettext '$YOUR_APP_NAME Deployment Management Script '
echo
echo "Usage: "
echo " ./manager.sh [COMMAND] "
echo " ./manager.sh --help"
echo
echo "Management Commands [# 服務管理指令]: "
echo " start $(gettext 'Start $YOUR_APP_NAME [# 啟動]')"
echo " stop $(gettext 'Stop $YOUR_APP_NAME [# 停止]')"
echo " restart $(gettext 'Restart $YOUR_APP_NAME [# 重啟]')"
echo " status $(gettext 'Check $YOUR_APP_NAME [# 檢查狀態]')"
echo " uninstall $(gettext 'Uninstall $YOUR_APP_NAME [# 解除安裝] ')"
echo
echo "Check Version Commands [# 檢視版本資訊]: "
echo " version $(gettext 'Version Info [# 檢視軟體版本資訊]')"
}
##############################################################################
# main
if [ $(id -u) -ne "0" ]; then
log_error "ERROR: This script must be run as root." 1>&2
exit 1
fi
case $mode in
"start")
install
;;
"status")
status
;;
"stop")
stop
;;
"version")
version
;;
"restart")
restart_service
;;
"uninstall")
uninstall_service
;;
"help")
usage
;;
"--help")
usage
;;
"-h")
usage
;;
*)
echo "No such command: ${action}"
usage
;;
esac
exit 0
使用
-
啟動:
sh manager.sh start
# 輸出效果如下 [+] deploy the program ... [+] install done!
-
重啟:
sh manager.sh restart
# 輸出效果如下 [+] stop $YOUR_APP_NAME ... [+] deploy the program ... [+] install done! [+] restart done!
-
停止:
sh manager.sh stop
# 輸出如下 [+] stop $YOUR_APP_NAME ...
-
解除安裝:
sh manager.sh uninstall
-
檢視版本號:
sh manager.sh version
#輸出如下: XXX agent API service. The Version: 0.2.2
-
幫助手冊:
sh manager.sh --help
# 輸出如下 gettext $YOU_APP_NAME Deployment Management Script Usage: ./manager.sh [COMMAND] ./manager.sh --help Management Commands [# 服務管理指令]: start Start $YOU_APP_NAME [# 啟動] stop Stop $YOU_APP_NAME [# 停止] restart Restart $YOU_APP_NAME [# 重啟] status Check $YOU_APP_NAME [# 檢查狀態] uninstall Uninstall $YOU_APP_NAME [# 解除安裝] Check Version Commands [# 檢視版本資訊]: version Version Info [# 檢視軟體版本資訊]