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
This commit is contained in:
Krzysztof kuhy Rudnicki 2025-12-04 20:31:44 +01:00
parent c2972c4c33
commit c95462c5db
2 changed files with 98 additions and 8 deletions

84
scripts/fixes/fix_systemctl.sh Executable file
View File

@ -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" <<EOF
[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
systemctl reset-failed "$SERVICE_NAME.service" 2>/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!"

View File

@ -9,30 +9,36 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ORGANIZE_SCRIPT="$SCRIPT_DIR/organize_downloads.sh" ORGANIZE_SCRIPT="$SCRIPT_DIR/organize_downloads.sh"
SERVICE_NAME="media-organizer" SERVICE_NAME="media-organizer"
SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service" 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 # Function to log messages
log() { log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"
} }
# Check if organize script exists # Check if organize script exists
if [[ ! -f $ORGANIZE_SCRIPT ]]; then if [[ ! -f $ORGANIZE_SCRIPT ]]; then
log "ERROR: organize_downloads.sh not found at $ORGANIZE_SCRIPT" log "ERROR: organize_downloads.sh not found at $ORGANIZE_SCRIPT"
exit 1 exit 1
fi fi
# Check if running as root for systemd service creation # Check if running as root for systemd service creation
if [[ $EUID -ne 0 ]]; then if [[ $EUID -ne 0 ]]; then
log "This script needs to be run as root to create systemd service." log "This script needs to be run as root to create systemd service."
log "Please run: sudo $0" log "Re-executing with sudo..."
exit 1 exec sudo "$0" "$@"
fi fi
log "Setting up media organizer startup service..." log "Setting up media organizer startup service..."
# Create systemd service file # Create systemd service file
cat > "$SERVICE_FILE" << EOF cat >"$SERVICE_FILE" <<EOF
[Unit] [Unit]
Description=Media File Organizer Description=Media File Organizer
After=graphical-session.target After=graphical-session.target