aboutsummaryrefslogtreecommitdiffstats
path: root/system/usr/polybar-gpu-trigger
blob: 55b52fc1e25e8e7fc7fd03448d84210ddd676608 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/bin/bash
# /usr/local/bin/polybar-gpu-trigger

# 1. Critical sleep to prevent the udev timing race condition
sleep 0.5

# Find the Process ID (PID) of Polybar
POLYBAR_PID=$(pgrep -x polybar)
if [ -z "$POLYBAR_PID" ]; then
    exit 0
fi

# Loop through found PIDs to locate the correct user context
for pid in $POLYBAR_PID; do
    if [ -f "/proc/$pid/environ" ]; then
        USER_UID=$(stat -c "%u" "/proc/$pid")
        USER_NAME=$(id -nu "$USER_UID")

        # Safely parse the environment file for XDG_RUNTIME_DIR
        XDG_DIR=$(tr '\0' '\n' < "/proc/$pid/environ" | grep '^XDG_RUNTIME_DIR=' | cut -d= -f2-)

        if [ -n "$XDG_DIR" ]; then
            # Drop root privileges and forcefully send the text directly into Polybar
            sudo -u "$USER_NAME" XDG_RUNTIME_DIR="$XDG_DIR" \
                polybar-msg action "#gpu-status.send.0" >/dev/null 2>&1
            break
        fi
    fi
done