From c95462c5db45ed3fdd746bf803f678b14295bf1d Mon Sep 17 00:00:00 2001 From: Krzysztof kuhy Rudnicki Date: Thu, 4 Dec 2025 20:31:44 +0100 Subject: [PATCH] fix: repair media-organizer.service and prevent future issues - Add fix_systemctl.sh to repair corrupted media-organizer.service - Fix setup_media_organizer.sh to use SUDO_USER instead of whoami when running with sudo (prevents User/Group being set to root) The service was failing due to: 1. Corrupted ExecStart path (line break in the middle) 2. Wrong script path (missing 'utils/' directory) 3. User/Group set to root instead of actual user --- scripts/fixes/fix_systemctl.sh | 84 ++++++++++++++++++++++++++ scripts/utils/setup_media_organizer.sh | 22 ++++--- 2 files changed, 98 insertions(+), 8 deletions(-) create mode 100755 scripts/fixes/fix_systemctl.sh diff --git a/scripts/fixes/fix_systemctl.sh b/scripts/fixes/fix_systemctl.sh new file mode 100755 index 0000000..b663d42 --- /dev/null +++ b/scripts/fixes/fix_systemctl.sh @@ -0,0 +1,84 @@ +#!/bin/bash + +# Fix script for media-organizer.service +# The service was failing due to: +# 1. Corrupted ExecStart path (line break in the middle) +# 2. Wrong script path (missing 'utils/' directory) +# 3. User/Group set to root instead of actual user + +set -euo pipefail + +SERVICE_NAME="media-organizer" +SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service" +ORGANIZE_SCRIPT="/home/kuhy/linux-configuration/scripts/utils/organize_downloads.sh" +TARGET_USER="kuhy" + +log() { + echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" +} + +# Check if running as root +if [[ $EUID -ne 0 ]]; then + log "This script needs to be run as root." + log "Re-executing with sudo..." + exec sudo "$0" "$@" +fi + +log "Fixing media-organizer.service..." + +# Verify the organize_downloads.sh script exists +if [[ ! -f $ORGANIZE_SCRIPT ]]; then + log "ERROR: organize_downloads.sh not found at $ORGANIZE_SCRIPT" + exit 1 +fi + +# Stop the service if running (ignore errors) +systemctl stop "$SERVICE_NAME.service" 2>/dev/null || true + +# Recreate the service file with correct configuration +cat >"$SERVICE_FILE" </dev/null || true +log "Reset failed state" + +# Re-enable the service +systemctl enable "$SERVICE_NAME.service" +log "Service enabled" + +# Optionally start the service to verify it works +log "Starting service to verify fix..." +if systemctl start "$SERVICE_NAME.service"; then + log "SUCCESS: media-organizer.service started successfully!" +else + log "WARNING: Service still has issues. Check: journalctl -u $SERVICE_NAME" +fi + +# Show current status +log "Current service status:" +systemctl status "$SERVICE_NAME.service" --no-pager || true + +log "Fix complete!" diff --git a/scripts/utils/setup_media_organizer.sh b/scripts/utils/setup_media_organizer.sh index 6b8c149..468d4be 100755 --- a/scripts/utils/setup_media_organizer.sh +++ b/scripts/utils/setup_media_organizer.sh @@ -9,30 +9,36 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" ORGANIZE_SCRIPT="$SCRIPT_DIR/organize_downloads.sh" SERVICE_NAME="media-organizer" SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service" -USER_NAME="$(whoami)" + +# Get the actual user (not root when running with sudo) +if [[ -n ${SUDO_USER:-} ]]; then + USER_NAME="$SUDO_USER" +else + USER_NAME="$(whoami)" +fi # Function to log messages log() { - echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" + echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" } # Check if organize script exists if [[ ! -f $ORGANIZE_SCRIPT ]]; then - log "ERROR: organize_downloads.sh not found at $ORGANIZE_SCRIPT" - exit 1 + log "ERROR: organize_downloads.sh not found at $ORGANIZE_SCRIPT" + exit 1 fi # Check if running as root for systemd service creation if [[ $EUID -ne 0 ]]; then - log "This script needs to be run as root to create systemd service." - log "Please run: sudo $0" - exit 1 + log "This script needs to be run as root to create systemd service." + log "Re-executing with sudo..." + exec sudo "$0" "$@" fi log "Setting up media organizer startup service..." # Create systemd service file -cat > "$SERVICE_FILE" << EOF +cat >"$SERVICE_FILE" <