bgnotify global threshold limits per-command control in Oh My Zsh
0 reputation · 29 Oct 2023, 14:22 UTC
0 reputation · 29 Oct 2023, 14:22 UTC
Goal is to use Oh My Zsh bgnotify plugin to receive desktop notifications for long-running foreground commands while keeping alert noise low.
bgnotify emits a notification when a command exceeds a time threshold and the terminal is unfocused. Noise reduction relies on the global bgnotify_threshold variable and the unfocused-window check. Backends differ by platform and availability. There is no documented per-command allowlist or denylist in the plugin.
This leaves an unresolved design choice between a single global threshold that is simple but may still notify for interactive sessions, and more precise control that would require custom hooks outside bgnotify.
What is the supported way to suppress notifications for specific commands without changing the global threshold? Does bgnotify expose any documented variable or hook for command filtering? How does backend availability affect the unfocused-window behavior on different platforms?
31775 reputation · 29 Oct 2023, 23:24 UTC
The plugin exposes a single variable, bgnotify_threshold, that controls how many seconds a foreground command must run before a notification is emitted when the terminal window is not focused. There is no documented allowlist, denylist, or hook that lets you filter commands on a per‑command basis.
Because the plugin has no built‑in filter, the only supported technique is to temporarily adjust bgnotify_threshold around the command you want to silence. The adjustment can be done inline or via an alias that restores the original value afterward.
# Temporarily raise the threshold to “never” for a single invocation
bgnotify_threshold=0; your_long_command_here; bgnotify_threshold=120
Or create a reusable alias:
alias silent_long='bgnotify_threshold=0; "$@"; bgnotify_threshold=120'
# Usage: silent_long sleep 120
Because bgnotify_threshold is read once per command, the wrapper must restore the original value before the command finishes. If you forget to restore it, subsequent commands will inherit the altered threshold.
At the time of writing, the bgnotify source code contains only the global threshold variable and the unfocused‑window check. No exported function or variable is provided for user‑defined filters. Future releases might add such a feature, but it is not present in the current stable version.
bgnotify chooses a backend automatically based on the platform:
notify-send (libnotify) and xdotool to query X11 focus. On Wayland, xdotool is typically unavailable, so the unfocused check fails and notifications are silently disabled.osascript and System Events to determine if the terminal is frontmost.snoretoast to send notifications and query the foreground process.If the required external utilities are missing, bgnotify falls back to no‑op mode, meaning no notifications will appear even for long commands.
echo $bgnotify_threshold
# Linux
which notify-send && which xdotool
# macOS
which osascript
# Windows
powershell -Command "Get-Item -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Run'
silent_long sleep 120 while the terminal is unfocused and verify that no notification appears.To give more precise guidance, could you let me know which display server you are using on Linux (X11 or Wayland)? That determines whether the X11‑based unfocused check will work.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.