2025-12-04 20:31:44 +01:00
|
|
|
#!/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"
|
2026-02-20 00:13:25 +01:00
|
|
|
|
|
|
|
|
DEFAULT_ORGANIZE_SCRIPT="/home/kuhy/testsAndMisc/linux_configuration/scripts/utils/organize_downloads.sh"
|
|
|
|
|
LEGACY_ORGANIZE_SCRIPT="/home/kuhy/linux-configuration/scripts/utils/organize_downloads.sh"
|
|
|
|
|
if [[ -f $DEFAULT_ORGANIZE_SCRIPT ]]; then
|
|
|
|
|
ORGANIZE_SCRIPT="$DEFAULT_ORGANIZE_SCRIPT"
|
|
|
|
|
elif [[ -f $LEGACY_ORGANIZE_SCRIPT ]]; then
|
|
|
|
|
ORGANIZE_SCRIPT="$LEGACY_ORGANIZE_SCRIPT"
|
|
|
|
|
else
|
|
|
|
|
ORGANIZE_SCRIPT="$DEFAULT_ORGANIZE_SCRIPT"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
TARGET_USER="${SUDO_USER:-kuhy}"
|
2025-12-04 20:31:44 +01:00
|
|
|
|
|
|
|
|
log() {
|
2026-01-07 22:52:20 +01:00
|
|
|
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"
|
2025-12-04 20:31:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Check if running as root
|
|
|
|
|
if [[ $EUID -ne 0 ]]; then
|
2026-01-07 22:52:20 +01:00
|
|
|
log "This script needs to be run as root."
|
|
|
|
|
log "Re-executing with sudo..."
|
|
|
|
|
exec sudo "$0" "$@"
|
2025-12-04 20:31:44 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
log "Fixing media-organizer.service..."
|
|
|
|
|
|
|
|
|
|
# Verify the organize_downloads.sh script exists
|
|
|
|
|
if [[ ! -f $ORGANIZE_SCRIPT ]]; then
|
2026-01-07 22:52:20 +01:00
|
|
|
log "ERROR: organize_downloads.sh not found at $ORGANIZE_SCRIPT"
|
|
|
|
|
exit 1
|
2025-12-04 20:31:44 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Stop the service if running (ignore errors)
|
2026-01-07 22:52:20 +01:00
|
|
|
systemctl stop "$SERVICE_NAME.service" 2> /dev/null || true
|
2025-12-04 20:31:44 +01:00
|
|
|
|
|
|
|
|
# Recreate the service file with correct configuration
|
2026-01-07 22:52:20 +01:00
|
|
|
cat > "$SERVICE_FILE" << EOF
|
2025-12-04 20:31:44 +01:00
|
|
|
[Unit]
|
|
|
|
|
Description=Media File Organizer
|
|
|
|
|
After=graphical-session.target
|
|
|
|
|
Wants=graphical-session.target
|
|
|
|
|
|
|
|
|
|
[Service]
|
|
|
|
|
Type=oneshot
|
|
|
|
|
User=$TARGET_USER
|
|
|
|
|
Group=$TARGET_USER
|
|
|
|
|
ExecStart=$ORGANIZE_SCRIPT
|
|
|
|
|
StandardOutput=journal
|
|
|
|
|
StandardError=journal
|
|
|
|
|
RemainAfterExit=no
|
|
|
|
|
|
|
|
|
|
[Install]
|
|
|
|
|
WantedBy=multi-user.target
|
|
|
|
|
EOF
|
|
|
|
|
|
|
|
|
|
log "Recreated service file: $SERVICE_FILE"
|
|
|
|
|
|
|
|
|
|
# Reload systemd daemon
|
|
|
|
|
systemctl daemon-reload
|
|
|
|
|
log "Reloaded systemd daemon"
|
|
|
|
|
|
|
|
|
|
# Reset the failed state
|
2026-01-07 22:52:20 +01:00
|
|
|
systemctl reset-failed "$SERVICE_NAME.service" 2> /dev/null || true
|
2025-12-04 20:31:44 +01:00
|
|
|
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
|
2026-01-07 22:52:20 +01:00
|
|
|
log "SUCCESS: media-organizer.service started successfully!"
|
2025-12-04 20:31:44 +01:00
|
|
|
else
|
2026-01-07 22:52:20 +01:00
|
|
|
log "WARNING: Service still has issues. Check: journalctl -u $SERVICE_NAME"
|
2025-12-04 20:31:44 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Show current status
|
|
|
|
|
log "Current service status:"
|
|
|
|
|
systemctl status "$SERVICE_NAME.service" --no-pager || true
|
|
|
|
|
|
|
|
|
|
log "Fix complete!"
|