I have a old macbook running as a server. i dont want to blow out the display so i want it to shut off after a couple of seconds. i dont run X so i dont have any tooling running.
Lets hack the planet!
apt install evtest
#!/bin/bash
BRIGHTNESS_PATH="/sys/class/backlight/intel_backlight"
# IDLE_SECONDS=300 # 5 minutes
IDLE_SECONDS=30
BRIGHTNESS_OFF=0
TIMESTAMP_FILE="/tmp/last_input_activity"
# Store max brightness
MAX_BRIGHTNESS=$(cat "$BRIGHTNESS_PATH/max_brightness")
# Write current time to timestamp file
touch "$TIMESTAMP_FILE"
date +%s > "$TIMESTAMP_FILE"
monitor_input() {
local device=$1
evtest "$device" | while read -r line; do
if [[ "$line" == *"Event"* ]]; then
date +%s > "$TIMESTAMP_FILE"
fi
done
}
# Start background monitors (adjust devices to match your keyboard/mouse)
monitor_input /dev/input/event4 & # ← change if needed
# monitor_input /dev/input/event3 & # ← change if needed
# Main idle check loop
while true; do
if [[ -f "$TIMESTAMP_FILE" ]]; then
LAST_ACTIVITY=$(cat "$TIMESTAMP_FILE")
else
LAST_ACTIVITY=$(date +%s)
fi
NOW=$(date +%s)
IDLE_TIME=$((NOW - LAST_ACTIVITY))
CUR_BRIGHTNESS=$(cat "$BRIGHTNESS_PATH/brightness")
if [ "$IDLE_TIME" -ge "$IDLE_SECONDS" ]; then
if [ "$CUR_BRIGHTNESS" -ne "$BRIGHTNESS_OFF" ]; then
echo "$BRIGHTNESS_OFF" | sudo tee "$BRIGHTNESS_PATH/brightness" > /dev/null
fi
else
if [ "$CUR_BRIGHTNESS" -eq "$BRIGHTNESS_OFF" ]; then
echo "$MAX_BRIGHTNESS" | sudo tee "$BRIGHTNESS_PATH/brightness" > /dev/null
fi
fi
sleep 2
done
[Unit]
Description=Idle screen dimmer (evtest-based)
After=multi-user.target
[Service]
ExecStart=/home/xantios/DisplayHack/script.sh
Restart=always
[Install]
WantedBy=multi-user.target
chmod +x ./script.sh
sudo systemctl daemon-reexec
sudo systemctl enable input-idle-dimmer.service
sudo systemctl start input-idle-dimmer.service