深度思维者

永远年轻,永远热泪盈眶

通用程序部署shell脚本

简介

通常一个独立的服务,程序,中间件成后,为便于测试,上线部署都会编写一一个shell用于对程序的启动,重启 ,查看状态,停止,卸载 等动作, 以下是 一个使用shell脚本来实现的简单demo, 直接见代码。


功能

  1. 启动: start
  2. 重启: restart
  3. 停止:stop
  4. 卸载:uninstall
  5. 查看运行状态: status
  6. 获取程序版本号: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

使用

  1. 启动: sh manager.sh start

    # 输出效果如下
    [+] deploy the program ...
    [+] install done!
    
  2. 重启:sh manager.sh restart

    # 输出效果如下
    [+] stop $YOUR_APP_NAME ...
    [+] deploy the program ...
    [+] install done!
    [+] restart done!
    
  3. 停止: sh manager.sh stop

    # 输出如下
    [+] stop $YOUR_APP_NAME ...
    
  4. 卸载: sh manager.sh uninstall

  5. 查看版本号: sh manager.sh version

    #输出如下:
    XXX agent API service.
    The Version: 0.2.2
    
  6. 帮助手册: 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                     [# 查看软件版本信息]
    
    
posted @ 2024-05-31 11:25  failymao  阅读(72)  评论(0编辑  收藏  举报