Files
BetterDesk/dev_modules/fix_systemd_services.sh
T
UNITRONIX bc7680ac4c Improve systemd service fix and update scripts
Enhanced both fix_systemd_services.sh and install-improved.sh to better handle missing ExecStart lines, robustly detect and update service binaries to API-enabled versions, and provide clearer status and backup information. The scripts now create timestamped backups, handle more edge cases, and give improved user feedback for manual intervention if needed.
2026-02-02 14:25:34 +01:00

277 lines
8.7 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
# =============================================================================
# BetterDesk - Fix Systemd Services for HBBS/HBBR
# =============================================================================
# This script checks and fixes systemd service files to use the modified
# hbbs-v8-api and hbbr-v8-api binaries with API support.
#
# Handles cases where:
# - ExecStart is missing entirely
# - ExecStart uses original hbbs/hbbr binaries
# - ExecStart already uses hbbs-v8-api/hbbr-v8-api
#
# Usage: sudo ./fix_systemd_services.sh [RUSTDESK_PATH]
# =============================================================================
set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
print_success() { echo -e "${GREEN}$1${NC}"; }
print_error() { echo -e "${RED}$1${NC}"; }
print_warning() { echo -e "${YELLOW}$1${NC}"; }
print_info() { echo -e "${BLUE} $1${NC}"; }
# Default RustDesk path
RUSTDESK_PATH="${1:-/opt/rustdesk}"
echo ""
echo -e "${BLUE}=====================================${NC}"
echo -e "${BLUE}BetterDesk - Fix Systemd Services${NC}"
echo -e "${BLUE}=====================================${NC}"
echo ""
# Check if running as root
if [ "$EUID" -ne 0 ]; then
print_error "This script must be run as root (sudo)"
exit 1
fi
# Common service names
HBBS_SERVICES=("rustdesksignal.service" "hbbs.service" "rustdesk-hbbs.service")
HBBR_SERVICES=("rustdeskrelay.service" "hbbr.service" "rustdesk-hbbr.service")
echo "RustDesk path: $RUSTDESK_PATH"
echo ""
# =============================================================================
# Check binaries
# =============================================================================
print_info "Checking for API-enabled binaries..."
HBBS_BINARY=""
HBBR_BINARY=""
if [ -f "$RUSTDESK_PATH/hbbs-v8-api" ]; then
HBBS_BINARY="$RUSTDESK_PATH/hbbs-v8-api"
print_success "Found hbbs-v8-api: $HBBS_BINARY"
else
print_error "hbbs-v8-api not found in $RUSTDESK_PATH"
fi
if [ -f "$RUSTDESK_PATH/hbbr-v8-api" ]; then
HBBR_BINARY="$RUSTDESK_PATH/hbbr-v8-api"
print_success "Found hbbr-v8-api: $HBBR_BINARY"
else
print_error "hbbr-v8-api not found in $RUSTDESK_PATH"
fi
if [ -z "$HBBS_BINARY" ] && [ -z "$HBBR_BINARY" ]; then
print_error "No API-enabled binaries found!"
print_info "Please copy hbbs-v8-api and hbbr-v8-api to $RUSTDESK_PATH first"
exit 1
fi
echo ""
# =============================================================================
# Check and display current services
# =============================================================================
print_info "Scanning for RustDesk services..."
echo ""
found_services=()
services_need_fix=()
for service in "${HBBS_SERVICES[@]}" "${HBBR_SERVICES[@]}"; do
if [ -f "/etc/systemd/system/$service" ]; then
found_services+=("$service")
local exec_start=$(grep "^ExecStart=" "/etc/systemd/system/$service" 2>/dev/null || echo "")
local work_dir=$(grep "^WorkingDirectory=" "/etc/systemd/system/$service" 2>/dev/null || echo "")
local user=$(grep "^User=" "/etc/systemd/system/$service" 2>/dev/null || echo "")
echo "Found: $service"
echo " Status: $(systemctl is-active "$service" 2>/dev/null || echo 'unknown')"
if [ -n "$exec_start" ]; then
echo " $exec_start"
if echo "$exec_start" | grep -q "v8-api"; then
echo -e " ${GREEN}✓ Already using API binary${NC}"
else
echo -e " ${YELLOW}⚠ Needs update to use API binary${NC}"
services_need_fix+=("$service")
fi
else
echo -e " ${RED}ExecStart: MISSING!${NC}"
services_need_fix+=("$service")
fi
[ -n "$work_dir" ] && echo " $work_dir"
[ -n "$user" ] && echo " $user"
echo ""
fi
done
if [ ${#found_services[@]} -eq 0 ]; then
print_warning "No RustDesk services found!"
echo ""
print_info "Looking for any rustdesk-related services..."
find /etc/systemd/system -name "*rustdesk*" -o -name "*hbb*" 2>/dev/null | while read -r f; do
echo " Found: $f"
done
exit 1
fi
if [ ${#services_need_fix[@]} -eq 0 ]; then
print_success "All services are already configured correctly!"
exit 0
fi
# =============================================================================
# Fix services
# =============================================================================
echo ""
print_info "Services that need fixing: ${services_need_fix[*]}"
echo ""
read -p "Do you want to fix these services? [y/N] " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
print_info "Aborted by user"
exit 0
fi
echo ""
services_updated=false
for service in "${services_need_fix[@]}"; do
service_file="/etc/systemd/system/$service"
print_info "Processing $service..."
# Create timestamped backup
backup_name="${service}.backup.$(date +%Y%m%d_%H%M%S)"
cp "$service_file" "/etc/systemd/system/$backup_name"
print_success "Created backup: $backup_name"
# Determine which binary to use
binary=""
if [[ " ${HBBS_SERVICES[*]} " =~ " ${service} " ]]; then
binary="$HBBS_BINARY"
binary_type="hbbs"
else
binary="$HBBR_BINARY"
binary_type="hbbr"
fi
if [ -z "$binary" ]; then
print_error "No $binary_type-v8-api binary available, skipping $service"
continue
fi
# Get current ExecStart
current_exec=$(grep "^ExecStart=" "$service_file" 2>/dev/null || echo "")
if [ -z "$current_exec" ]; then
# NO ExecStart - need to add it
print_warning "No ExecStart found - adding it..."
# Add ExecStart after [Service] line
sed -i "/^\[Service\]/a ExecStart=$binary" "$service_file"
print_success "Added: ExecStart=$binary"
services_updated=true
elif echo "$current_exec" | grep -q "${binary_type}-v8-api"; then
print_success "Already using ${binary_type}-v8-api"
else
# Update existing ExecStart
echo " Current: $current_exec"
# Replace binary path
new_exec=$(echo "$current_exec" | sed -E "s|/${binary_type}([[:space:]]|\$|-)|/${binary_type}-v8-api\1|g")
if [ "$current_exec" != "$new_exec" ]; then
sed -i "s|^ExecStart=.*|$new_exec|" "$service_file"
print_success "Updated: $new_exec"
services_updated=true
else
# If sed didn't change anything, try replacing the whole ExecStart
print_warning "Pattern replacement failed, replacing entire ExecStart..."
sed -i "s|^ExecStart=.*|ExecStart=$binary|" "$service_file"
print_success "Set: ExecStart=$binary"
services_updated=true
fi
fi
echo ""
done
# =============================================================================
# Reload and restart
# =============================================================================
if [ "$services_updated" = true ]; then
print_info "Reloading systemd daemon..."
systemctl daemon-reload
print_success "Systemd reloaded"
echo ""
read -p "Do you want to restart the fixed services now? [y/N] " -n 1 -r
echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
for service in "${services_need_fix[@]}"; do
print_info "Restarting $service..."
systemctl restart "$service" 2>/dev/null || true
sleep 1
if systemctl is-active "$service" &>/dev/null; then
print_success "$service is running"
else
print_error "$service failed to start"
print_info "Check logs: journalctl -u $service -n 20"
fi
done
fi
fi
# =============================================================================
# Final status
# =============================================================================
echo ""
print_info "Final Service Status:"
echo ""
for service in "${found_services[@]}"; do
status=$(systemctl is-active "$service" 2>/dev/null || echo "inactive")
exec_start=$(grep "^ExecStart=" "/etc/systemd/system/$service" 2>/dev/null | head -1)
if [ "$status" = "active" ]; then
echo -e "${GREEN}${NC} $service - running"
else
echo -e "${RED}${NC} $service - $status"
fi
if [ -n "$exec_start" ]; then
echo " $exec_start"
else
echo -e " ${RED}ExecStart: STILL MISSING!${NC}"
fi
echo ""
done
print_success "Done!"
echo ""
print_info "Backups saved in /etc/systemd/system/*.backup.*"
print_info "To restore: cp /etc/systemd/system/SERVICE.backup.TIMESTAMP /etc/systemd/system/SERVICE"
echo ""