效果图

1. 执行config.sh注册sirius和sirius系统服务

将java -jar xxx.jar注册为系统服务,安装sirius

image

2. 使用sirius工具管理服务

image
image

3. 使用systemctl或service工具管理服务

image

实现思路

1. 制作管理工具,我这里的名字叫做sirius

shell脚本在linux下可以直接被系统当做可执行文件执行
所以我们编写管理工具,直接放到常用的PATH即可
这里我把sirius放到/usr/bin/

2. 制作系统服务文件,sirius.service

你可以是myser.service
myser是服务名称,文件名是什么服务名就是什么

3. 制作一键配置脚本

具体内容看脚本实现吧

代码

1. config.sh

用于自动配置环境和注册服务
#!/bin/bash
# author :    yiming
# time   :    20230225
# url    :    api.wer.plus
# github :    HG-ha
cat << EOF

    # -------------------------- About this script ------------------------- #
    #         This script automatically configures sirius commands.          #
    #  When the running path of the program changes, re-execute the script.  #
    #    Once this script is successful, you can use the sirius command.     #
    #     Executing this script overwrites the previous configuration.       #
    # -------------------------- About this script ------------------------- #

EOF

# Specify the sirius workspace as the current path.

sirius_work=`pwd`

# Obtain the current user to save related configurations.

sirius_user=${USER}

# Check whether the user configuration path exists.

if [ ${USER} == "root" ];
  # Root workspace
  then 
    sirius_config_path=/${USER}/.config/sirius/
    if [ -d ${sirius_config_path} ];
      then
        echo "" > ${sirius_config_path}config
        echo -e "\e[32m\tGet Workspace ["${sirius_config_path}"]\e[0m"
        sirius_path_conf=/${USER}/.bashrc
      else
        mkdir -p ${sirius_config_path}
        echo -e "\e[32m\tCreate Workspace ["${sirius_config_path}"] \e[0m"
        sirius_path_conf=/${USER}/.bashrc
    fi
  # Other workspace
  else
    sirius_config_path=/home/${USER}/.config/sirius/
    if [ -d ${sirius_config_path} ];
      then
        echo "" > ${sirius_config_path}config
        echo -e "\e[32m\tGet Workspace ["${sirius_config_path}"]"
        sirius_path_conf=/home/${USER}/.bashrc
      else
        mkdir -p ${sirius_config_path}
        echo -e "\e[32m\tCreate Workspace ["${sirius_config_path}"]"
        sirius_path_conf=/home/${USER}/.bashrc
    fi
fi


# Get the jar package for the current version of Sirius-Pro.

sirius_pro_jar=`find ${sirius_work} -name "SiriusPro*.jar"`
sirius_pro_jdk=${sirius_work}"/jdk/bin/java"
echo -e "\e[32m\tJdk for sirius ["${sirius_pro_jdk}"] \e[0m"
echo -e "\e[32m\tGet the current version ["${sirius_pro_jar}"] \e[0m"

# Write configuration to ${sirius_config_path}
echo "sirius_work "${sirius_work} >> ${sirius_config_path}config
echo "sirius_pro_jdk "${sirius_pro_jdk} >> ${sirius_config_path}config
echo "sirius_pro_jar "${sirius_pro_jar} >> ${sirius_config_path}config
echo "99999" > ${sirius_config_path}pid
echo -e "\e[32m\tWrite configuration complete \e[0m"


# Create command sirius

cp ${sirius_work}/sirius /usr/bin/
chmod +x /usr/bin/sirius
echo -e "\e[32m\tThe sirius command is created \e[0m"

# Registration system service
cp sirius.service /etc/systemd/system/
chmod 745 /etc/systemd/system/sirius.service
systemctl daemon-reload

# Create environment variables
#sed -i '/^sirius_pro_jdk/d' ${sirius_path_conf}
#sed -i '/^sirius_pro_jar/d' ${sirius_path_conf}
#echo "sirius_pro_jdk=`grep "sirius_pro_jdk" ${sirius_config_path}config | awk '{print $2}'`" >> ${sirius_path_conf}
#echo "sirius_pro_jar=`grep "sirius_pro_jar" ${sirius_config_path}config | awk '{print $2}'`" >> ${sirius_path_conf}

#export sirius_pro_jdk=${sirius_pro_jdk}
#export sirius_pro_jar=${sirius_pro_jar}

# 
echo -e "\n\tYou are now ready to start the service using sirius start"
echo -e "\tLike this:"
echo -e "\t\t\e[36msirius start \e[0m"
echo -e "\tOr:"
echo -e "\t\t\e[36m./sirius start \e[0m"

2. sirius

专有工具,用于给用户提供直接管理方式,也为systemctl、service工具提供管理接口
#!/bin/bash
# author :    yiming
# time   :    20230225
# url    :    api.wer.plus
# github :    HG-ha

# -------------------------- About this script ------------------------- #
#                         jar for managing sirius                        #
# -------------------------- About this script ------------------------- #

rUSER=`whoami`

grep "Red Hat" /proc/version >> /dev/null
if [[ $? -eq 0 ]];then System_release="Red Hat";fi
grep "Ubuntu" /proc/version >> /dev/null
if [[ $? -eq 0 ]];then System_release="Ubuntu";fi


if [ ${rUSER} == "root" ]
  # Root workspace
  then
    sirius_pro_jdk=`awk '/sirius_pro_jdk/ {print $2}' /root/.config/sirius/config `
    sirius_pro_jar=`awk '/sirius_pro_jar/ {print $2}' /root/.config/sirius/config `
    sirius_pro_pid=`cat /root/.config/sirius/pid`
    sirius_pro_pidf="/root/.config/sirius/pid"
    sirius_work=`awk '/sirius_work/ {print $2}' /root/.config/sirius/config `
  # Other workspace
  else
    sirius_pro_jdk=`awk '/sirius_pro_jdk/ {print $2}' /home/${rUSER}/.config/sirius/config `
    sirius_pro_jar=`awk '/sirius_pro_jar/ {print $2}' /home/${rUSER}/.config/sirius/config `
    sirius_pro_pid=`cat /home/${rUSER}/.config/sirius/pid`
    sirius_pro_pidf="/home/${rUSER}/.config/sirius/pid"
    sirius_work=`awk '/sirius_work/ {print $2}' /home/${rUSER}/.config/sirius/config `
fi



sirius_start (){
  ps -p ${sirius_pro_pid} | grep sirius >> /dev/null
  if [[ $? -eq 0 ]];
    then
      echo -e "\t\e[31mThere is already a running sirius.\e[0m"
      echo -e "\tPID:\e[32m"${sirius_pro_pid}"\e[0m"
    else
      echo $$ > ${sirius_pro_pidf}
      if [[ $1 == "-d" ]];
        then
          echo -e "\t\e[32msirius is running\e[0m"
          systemctl start sirius
        else
          echo -e "\t\e[32msirius is running\e[0m"
          cd ${sirius_work} && ${sirius_pro_jdk} -jar ${sirius_pro_jar}
      fi
  fi
}

sirius_start_system (){
echo $$ > ${sirius_pro_pidf}
cd ${sirius_work} && ${sirius_pro_jdk} -jar ${sirius_pro_jar}
}


sirius_stop (){
  ps -p ${sirius_pro_pid} | grep sirius >> /dev/null
  if [[ $? -eq 0 ]];
    then
      kill -9 ${sirius_pro_pid}
      echo -e "\t\e[32msirius is down\e[0m"
    else
      echo -e "\t\e[31mThere is no running sirius.\e[0m"
  fi
}

sirius_reload (){
  ps -p ${sirius_pro_pid} | grep sirius >> /dev/null
  if [[ $? -eq 0 ]];
    then
      kill -HUP ${sirius_pro_pid}
      echo -e "\t\e[32msirius reload complete\e[0m"
    else
      echo -e "\t\e[31mThere is no running sirius.\e[0m"
      echo -e "\t\e[31mPlease run sirius first.\e[0m"
  fi
}

sirius_restart (){
  ps -p ${sirius_pro_pid} | grep sirius >> /dev/null
  if [[ $? -eq 0 ]];
    then
      kill -9 ${sirius_pro_pid}
      sleep 2
      sirius start -d
      echo -e "\t\e[32msirius is running\e[0m"
    else
      sirius start -d
      echo -e "\t\e[32msirius is running\e[0m"
  fi
}


sirius_status (){
  ps -p ${sirius_pro_pid} | grep sirius >> /dev/null
  if [[ $? -eq 0 ]];
    then
      echo -e "\t\e[32msirius is running\e[0m"
    else
      echo -e "\t\e[31msirius not running\e[0m"
  fi
}

sirius_enable (){
#sed -i '/sirius start -d/d' /etc/profile
#echo "sirius start -d" >> /etc/profile
systemctl enable sirius
echo -e "\t\e[32mEnable bootstrap\e[0m"
}

sirius_disable (){
#sed -i '/sirius start -d/d' /etc/profile
systemctl disable sirius
echo -e "\t\e[32mDisable self-start\e[0m"
}



case $1 in

start)
  sirius_start $2
  ;;

starts)
  sirius_start_system
 ;;

stop)
  sirius_stop
  ;;

reload)
  sirius_reload
  ;;

restart)
  sirius_restart
  ;;

status)
  sirius_status
  ;;

enable)
  sirius_enable
  #systemctl enable sirius
  ;;

disable)
  sirius_disable
  #systemctl disable sirius
  ;;
*)
  echo -e "Support option:"
  echo -e "\t\e[32mstart\e[0m"
  echo -e "\t\tsirius start"
  echo -e "\t\tsirius start -d :Silent running, not available for first run"
  echo -e "\t\e[32mstop\e[0m"
  echo -e "\t\tsirius stop"
  echo -e "\t\e[32mreload\e[0m"
  echo -e "\t\tsirius reload"
  echo -e "\t\e[32mrestart\e[0m"
  echo -e "\t\tsirius restart"
  echo -e "\t\e[32mstatus\e[0m"
  echo -e "\t\tsirius status"
  echo -e "\t\e[32menable\e[0m"
  echo -e "\t\tsirius enable"
  echo -e "\t\e[32mdisable\e[0m"
  echo -e "\t\tsirius disable"

esac

3. sirius.service

注册系统服务用到的文件
[Unit]
Description= sirius Channel Robot Framework
# 网络服务一般等network启动成功后再运行
After=network.target
Wants=network.target

[Service]
ExecStart=/usr/bin/sirius starts
ExecReload=/usr/bin/sirius reload
ExecStop=/usr/bin/sirius stop

[Install]
WantedBy=multi-user.target