HM-SPMS/installer/others/sys_file_service_manager.sh

96 lines
2.3 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
set -e
#注册 sys_file_service 到 systemd
SERVICE_NAME="sys_file_service"
SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service"
script_path="/opt/EnergyHub/installer"
install_service() {
echo "INFO开始注册 ${SERVICE_NAME} 到 systemd..."
EXEC_PATH="$(dirname "$script_path")/product/$BIN_DIR_VER/sys_file_service"
WORK_DIR="$(dirname "$EXEC_PATH")"
if [ ! -f "$EXEC_PATH" ]; then
echo "ERROR: 未找到可执行文件 ${EXEC_PATH}"
exit 1
fi
chmod +x "$EXEC_PATH"
cat > "$SERVICE_FILE" <<EOL
[Unit]
Description=Sys Data Sync Server Service
After=network.target
[Service]
Type=simple
ExecStart=$EXEC_PATH
WorkingDirectory=$WORK_DIR
Restart=always
RestartSec=5
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOL
systemctl daemon-reload
systemctl enable ${SERVICE_NAME}.service
systemctl restart ${SERVICE_NAME}.service
if systemctl is-active --quiet ${SERVICE_NAME}.service; then
echo "INFO${SERVICE_NAME} 服务已经成功启动并设置为开机自启!"
else
echo "ERROR: ${SERVICE_NAME} 服务启动失败!查看日志请执行:"
echo "journalctl -u ${SERVICE_NAME}.service -f"
fi
}
uninstall_service() {
echo "INFO正在卸载 ${SERVICE_NAME} 服务..."
if systemctl is-active --quiet ${SERVICE_NAME}.service; then
systemctl stop ${SERVICE_NAME}.service
fi
systemctl disable ${SERVICE_NAME}.service || true
if [ -f "$SERVICE_FILE" ]; then
rm -f "$SERVICE_FILE"
systemctl daemon-reload
echo "INFO${SERVICE_NAME} 服务已成功卸载!"
else
echo "WARNING: ${SERVICE_NAME} 服务文件未找到,可能已经被删除。"
fi
}
if [ $# -lt 1 ]; then
echo "用法:$0 {install|uninstall} [BIN_DIR_VER]"
exit 1
fi
ACTION="$1"
BIN_DIR_VER="$2"
# install 时 BIN_DIR_VER 必须提供
if [ "$ACTION" = "install" ] && [ -z "$BIN_DIR_VER" ]; then
echo "ERROR: 安装时必须提供 BIN_DIR_VER 参数!"
echo "用法:$0 install BIN_DIR_VER"
exit 1
fi
case "$ACTION" in
install)
install_service
;;
uninstall)
uninstall_service
;;
*)
echo "用法:$0 {install|uninstall} [BIN_DIR_VER]"
exit 1
;;
esac