Skip to content

Looping Problem with yknotify Daemon – Needs Restart per Message #6

@amriunix

Description

@amriunix

Hi,

While integrating yknotify, I encountered a behavior that made it difficult to control its lifecycle in a scripting context.

Overview:
The binary appears to run as a daemon and streams output continuously, which is expected, but it does not exit after emitting a message. This causes a loop-related problem, As an example, if I cancel a Yubikey operation from the source of FIDO2 operation (Signing, 2FA, etc...), it keeps running for ever and I only want to handle one message at a time and then exit, then run it again and wait for the next event, but the current behavior forces me to implement workarounds by killing the process manually after reading one message.

Workaround:
I had to use a mkfifo and explicitly kill the process after receiving a single line:

#!/bin/bash

# Code inspired by: https://github.com/noperator/yknotify
# List of sounds: https://apple.stackexchange.com/a/479714

# Path to binaries
YKNTFY_BIN="/Users/<USER>/go/bin/yknotify"
TERM_NTFY_BIN="/opt/homebrew/bin/terminal-notifier"

# Delay enforcement
LAST_NTFY=0

while true; do
    # Start yknotify in background and capture its first line of output
    TEMP_FIFO=$(mktemp -u)
    mkfifo "$TEMP_FIFO"

    "$YKNTFY_BIN" > "$TEMP_FIFO" &
    YK_PID=$!

    # Read only the first line from yknotify
    if read -r line < "$TEMP_FIFO"; then
        # Kill the background yknotify process
        kill "$YK_PID" 2>/dev/null
        wait "$YK_PID" 2>/dev/null
    fi

    rm -f "$TEMP_FIFO"

    # Enforce 2-second cooldown
    NOW=$(date +%s)
    if [[ "$NOW" -le "$((LAST_NTFY + 2))" ]]; then
        continue
    fi
    LAST_NTFY="$NOW"

    # Parse message
    message=$(echo "$line" | jq -r '.type')
    ts=$(echo "$line" | jq -r '.ts')

    # Notify
    if [[ -x "$TERM_NTFY_BIN" ]]; then
        "$TERM_NTFY_BIN" -title "yknotify ($message)" -message "Please touch your YubiKey to continue." -sender com.yubico.ykman -sound default
    else
        osascript -e "display notification \"$message\" with title \"yknotify\""
    fi

    # Optional: brief pause
    sleep 0.2
done

Let me know if you’d like a patch suggestion. Thanks again for building this helpful tool!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions