Trying to create a systemd startup service

Deluge does not have a autostart feature. If you want it to autostart you need to create a systemd service for it, or add it to autostart in system settings. There is a way to set up a systemd timer to delay it’s start x amount of time, but I never got around to trying to figure out how to do it. For my purposes I’m better off just manually starting Deluge cause I run several Arch based OS’s and I need to go into Sonarr and or Radarr and remove items sometimes, so I need to have Deluge start after I’ve done that. There is one more service that one can create a timer in but off the top of my head I cannot remember it’s name, and it needs to be installed.

I think I’ve figured it out, but I’m still testing. As I realized late, you have the challenge of a requirement. :sweat_smile: You have a dependency - an ext. drive and you left it unaccounted for, also you tried to autostart a graphical application:

Create mount-dependent service
(`/etc/systemd/system/delugedelay-mount.service`)

[Unit]
Description=Delugedelay
After=multi-user.target
(Replace 'mnt-external.mount' with actual mount service)
After=mnt-external.mount
Requires=mnt-external.mount

[Service]
Type=simple
User=cccp
Group=cccp
ExecStart=/usr/bin/deluge
Restart=on-failure
RestartSec=10
Environment=DISPLAY=:0
Environment=XDG_RUNTIME_DIR=/run/user/1000 (if its correct ID)

[Install]
WantedBy=multi-user.target

Since you are no longer looking for a solution to this, I will look into it and try to find a solution.

For the “wannabeproblemsolver” in me

Was this the Arch package of Deluge? The Arch wiki entry mentions the systemd service file that comes with it.

So my Friend, i think i done :wink:

Preparation:
Checks if running as root - system permission required
Determines current user executing script with sudo to start Deluge application under this user. Disables and removes autostart configurations for Deluge to avoid conflicts.

Mount detection or time delay:
Attempts to detect external disk mounts (e.g. /dev/sdb, /dev/nvme) that are not root or boot directory. If external mounts are found, it creates delay script that waits for mounts to be available before starting Deluge (waiting time 4 min). If no external mounts are found, it falls back to fixed delay of 4 min. before Deluge is started.

Deluge Delay Script Creation:
It creates a bash script (~/.config/delugedelay.sh) that handles actual delayed startup of Deluge. Script checks if Deluge is already running to avoid double starts. Sets up a log file (/var/log/delugedelay.log) for activities of the Deluge service.

Systemd service creation:
Creates a systemd service file (/etc/systemd/system/delugedelay.service) that defines delayed start service. Service is configured to start according to network availability and local file system. Resolves conflicts with standard Deluge services (e.g. deluge.service, deluged.service).

Logging and activation:
Sets up a logrotate configuration for delugedelay.log to keep log files clean. Reloads systemd daemon and enables delugedelay.service to run on system startup.

Installation test and information:
At the end, test is performed to check validity of created script and activation of service. It issues cmds to start the service, check status and view logs.


If something is not working correctly, please let me know immediately.


#!/bin/bash

# ============================================================================
# DELUGE DELAY
# ============================================================================

set -euo pipefail

# Consolecolors
R='\033[0;31m'; G='\033[0;32m'; Y='\033[1;33m'; B='\033[0;34m'; N='\033[0m'

# Helper for output
log() { echo -e "${B}[$(date '+%H:%M:%S')]${N} $1"; }
error() { echo -e "${R}[ERROR]${N} $1" >&2; exit 1; }
warn() { echo -e "${Y}[WARN]${N} $1"; }
ok() { echo -e "${G}[OK]${N} $1"; }

# Check if script run as root
[[ $EUID -eq 0 ]] || error "Script must run as root"

# Determine config
REAL_USER=${SUDO_USER:-$(logname 2>/dev/null || echo "cccp")}
USER_HOME=$(eval echo "~$REAL_USER")
USER_ID=$(id -u "$REAL_USER")
SCRIPT_PATH="$USER_HOME/.config/delugedelay.sh"
SERVICE_PATH="/etc/systemd/system/delugedelay.service"

log "Setting up user: $REAL_USER (ID: $USER_ID)"

# Disable/remove existing Deluge autostart
log "Disabling existing Deluge autostart .."
find "$USER_HOME/.config/autostart" -name "*deluge*" -delete 2>/dev/null || true
systemctl list-unit-files | awk '/deluge.*\.service/ && !/delugedelay/ {print $1}' | while read -r svc; do
    systemctl disable "$svc" 2>/dev/null && systemctl stop "$svc" 2>/dev/null || true
done

# Detect ext. mounts
MOUNTS=$(mount | awk '/^\/dev\/(sd[b-z]|nvme[1-9])/ && $3 !~ /^\/(|boot)$/ {print $3}' || true)
if [[ -n "$MOUNTS" ]]; then
    ok "External mounts found: $(echo "$MOUNTS" | tr '\n' ' ')"
    MOUNT_CHECK="true"
else
    warn "No external mounts - using time-based delay"
    MOUNT_CHECK="false"
fi

# Create delay script
log "Creating delay script at $SCRIPT_PATH..."
mkdir -p "$USER_HOME/.config"

cat > "$SCRIPT_PATH" << EOF
#!/bin/bash
LOG="/var/log/delugedelay.log"
exec 1> >(tee -a "\$LOG") 2>&1

echo "\$(date): Start Deluge Delay Service (PID: \$\$)"

# Check Deluge running
if pgrep -x deluge >/dev/null; then
    echo "\$(date): Deluge running - exit service"
    exit 0
fi

EOF

if [[ "$MOUNT_CHECK" == "true" ]]; then
    cat >> "$SCRIPT_PATH" << EOF
MOUNTS=($MOUNTS)
for i in {1..24}; do
    all_mounted=true
    for mount in "\${MOUNTS[@]}"; do
        if ! mountpoint -q "\$mount" 2>/dev/null; then
            all_mounted=false
            break
        fi
    done
    if \$all_mounted; then
        echo "\$(date): Mounts available after \$((i*10)) seconds."
        break
    fi
    [[ \$i -eq 24 ]] && echo "\$(date): Timeout - starting Deluge anyway"
    sleep 10
done
EOF
else
    cat >> "$SCRIPT_PATH" << 'EOF'
echo "$(date): Waiting 240 seconds..."
sleep 240
EOF
fi

cat >> "$SCRIPT_PATH" << EOF
echo "\$(date): Starting Deluge as $REAL_USER..."
export DISPLAY=:0 XDG_RUNTIME_DIR="/run/user/$USER_ID" HOME="$USER_HOME"
runuser -l "$REAL_USER" -c "DISPLAY=:0 XDG_RUNTIME_DIR=/run/user/$USER_ID deluge" &
echo "\$(date): Deluge started"
EOF

chmod +x "$SCRIPT_PATH"
chown "$REAL_USER:$REAL_USER" "$SCRIPT_PATH"

# Create SystemD servicefile
log "Creating systemd service file at $SERVICE_PATH..."
cat > "$SERVICE_PATH" << EOF
[Unit]
Description=Deluge Delayed Startup
After=network-online.target local-fs.target
Wants=network-online.target
Conflicts=deluge.service deluged.service

[Service]
Type=simple
ExecStart=$SCRIPT_PATH
Restart=no
TimeoutStartSec=300
StandardOutput=journal
StandardError=journal
SyslogIdentifier=delugedelay

[Install]
WantedBy=multi-user.target
EOF

# Logging setup
log "Setting up logging for Deluge Delay..."
touch /var/log/delugedelay.log
chmod 644 /var/log/delugedelay.log

cat > /etc/logrotate.d/delugedelay << 'EOF'
/var/log/delugedelay.log {
    weekly
    rotate 4
    compress
    missingok
    notifempty
}
EOF

# Activate/Reload service
log "Reload systemd daemon and enable service 'delugedelay.service'..."
systemctl daemon-reload
systemctl enable delugedelay.service

# Installation test
if bash -n "$SCRIPT_PATH" && systemctl is-enabled delugedelay.service >/dev/null; then
    ok "Install successful"
else
    error "Install failed"
fi

# Concluding information
echo
ok "===COMPLETE==="
echo "Service: delugedelay.service"
echo "Script: $SCRIPT_PATH"
echo "Log: /var/log/delugedelay.log"
echo
echo "Other cmds:"
echo "Start: sudo systemctl start delugedelay.service"
echo "Status: sudo systemctl status delugedelay.service"
echo "Logs: sudo tail -f /var/log/delugedelay.log"
echo
warn "Reboot required to test"```
1 Like

OK copied it, created the delaydeluge.sh, dropped it in .config, made it executable, installed it, rebooted waited after mounting my drives and nothing.

You have Log for me?

Wed Jun 11 03:45:17 PM PDT 2025: Start Deluge Delay Service (PID: 7640)

I attempted it again with a reboot.

If you use deluge with Deamon and not GUI, try start deluged with mount-check. deluge GUI cannot always started properly at boot via systemd. Replace the start line in the script:

runuser -l "$REAL_USER" -c "DISPLAY=:0 XDG_RUNTIME_DIR=/run/user/$USER_ID deluge" &

by:

runuser -l "$REAL_USER" -c "deluged" &

And make sure that deluged is available as daemon.

Or maybe Unit isn’t loaded from boot target. The service is for multi-user.target:

[Install]
WantedBy=multi-user.target

This should be correct if system find target at boot time:

systemctl list-dependencies default.target

Possibly use graphical.target:

sudo systemctl edit delugedelay.service

and change:

[Install]
WantedBy=graphical.target

Then:

sudo systemctl daemon-reexec
sudo systemctl reenable delugedelay.service

The next option is a delayed start via autostart after login. We bypass systemd for Deluge (GUI) and try an autostart in user context.