---
name: lubuntu-monitor-power
description: Gestione alimentazione monitor su Lubuntu — standby, autostart, DPMS, xrandr
category: devops
---

# Lubuntu Monitor Power Management

Controlla lo standby del monitor su PC Lubuntu dove gira Hermes. Utile per risparmiare energia quando il PC rimane acceso 24/7 ma nessuno usa lo schermo.

## Background

Il DPMS (Display Power Management Signaling) su alcuni driver/hardware **non funziona correttamente** — il monitor non si spegne anche se `xset dpms` è configurato.

Soluzione robusta: **xrandr + xprintidle** — spegne il monitor via `xrandr --output NOME --off` e lo riaccende con `--auto`, controllando l'idle time di Xorg.

## Diagnosi — DPMS funziona?

```bash
export DISPLAY=:0
xset q | grep -A5 "DPMS"
```

Se mostra `DPMS is Disabled` o i timeout sono a 0, il DPMS non è attivo.

## Installazione

```bash
# xprintidle per leggere idle time
sudo apt install xprintidle -y
```

## Script monitor-standby.sh

Salvato in `/home/oem/.local/bin/monitor-standby.sh`:

```bash
#!/bin/bash
TIMEOUT_SEC=300  # 5 minuti
MONITOR="HDMI-3" # Cambiare con il proprio: xrandr --query | grep " connected"
DISPLAY=":0"
export DISPLAY="$DISPLAY"

IDLE_TIME=0
MONITOR_ON=true

while true; do
    IDLE=$(( $(xprintidle 2>/dev/null || echo "0") / 1000 ))
    if [ "$IDLE" -ge "$TIMEOUT_SEC" ] && [ "$MONITOR_ON" = true ]; then
        xrandr --output "$MONITOR" --off
        MONITOR_ON=false
    elif [ "$IDLE" -lt "$TIMEOUT_SEC" ] && [ "$MONITOR_ON" = false ]; then
        xrandr --output "$MONITOR" --auto
        MONITOR_ON=true
    fi
    sleep 30
done
```

**Attenzione:** identificare il nome del monitor con `xrandr --query | grep " connected"` (es. HDMI-1, DP-1, eDP-1, VGA-1).

## Autostart (persistente dopo riavvio)

```bash
mkdir -p ~/.config/autostart

cat > ~/.config/autostart/standby-monitor.desktop << 'DEOF'
[Desktop Entry]
Type=Application
Name=Standby Monitor 5min
Comment=Spegne monitor dopo 5 minuti di inattività
Exec=bash -c "sleep 10 && export DISPLAY=:0 && /home/oem/.local/bin/monitor-standby.sh"
Terminal=false
X-LXQt-Need-Tray=false
DEOF
```

## Test manuale

```bash
# Spegnere monitor
export DISPLAY=:0
xrandr --output HDMI-3 --off

# Riaccendere
xrandr --output HDMI-3 --auto
```

## Verifica funzionamento

```bash
# Script in esecuzione?
ps aux | grep monitor-standby | grep -v grep

# Se no, avviare via Hermes:
export DISPLAY=:0 && /home/oem/.local/bin/monitor-standby.sh
```

## Pitfall

- **Nomi monitor variabili**: se si cambia porta video (HDMI→DP), il nome cambia. Aggiornare `MONITOR="..."` nello script
- **xset dpms force off non funziona**: su alcuni driver (es. vecchi Intel/nouveau), il DPMS non è supportato a livello hardware. Usare sempre xrandr
- **xprintidle non installato**: il package si chiama proprio `xprintidle` (non `xidle` o `xprintidle-utils`)
- **DISPLAY=:0 mancante**: se lo script parte da autostart, la variabile `DISPLAY` non è impostata. Va esportata esplicitamente
- **Sessione X non su :0**: raro, verificare con `echo $DISPLAY` da terminale grafico
