Skip to content

Instantly share code, notes, and snippets.

@codejake
Created July 16, 2026 22:53
Show Gist options
  • Select an option

  • Save codejake/743abffa55ac224a08f841287a8c6ecd to your computer and use it in GitHub Desktop.

Select an option

Save codejake/743abffa55ac224a08f841287a8c6ecd to your computer and use it in GitHub Desktop.
here-i-am.md

This script shoudl be launched through SSH with sudo. It targets the currently logged-in macOS console user so audio plays through their session.

#!/bin/bash

set -u

INTERVAL=5
SOUND_FILE="/System/Library/Sounds/Sosumi.aiff"

if [[ $EUID -ne 0 ]]; then
    echo "Run this script with sudo." >&2
    exit 1
fi

if [[ ! -f "$SOUND_FILE" ]]; then
    echo "Sound file not found: $SOUND_FILE" >&2
    exit 1
fi

CONSOLE_USER="$(stat -f '%Su' /dev/console)"

if [[ -z "$CONSOLE_USER" || "$CONSOLE_USER" == "root" || "$CONSOLE_USER" == "loginwindow" ]]; then
    echo "No interactive macOS user is currently logged in." >&2
    exit 1
fi

CONSOLE_UID="$(id -u "$CONSOLE_USER")"

run_as_console_user() {
    launchctl asuser "$CONSOLE_UID" \
        sudo -u "$CONSOLE_USER" "$@"
}

echo "Setting volume to maximum."
echo "Playing a sound every ${INTERVAL} seconds as user: $CONSOLE_USER"
echo "Press Ctrl-C to stop."

while true; do
    # Keep the output volume at maximum and ensure it is unmuted.
    run_as_console_user osascript \
        -e 'set volume output volume 100' \
        -e 'set volume without output muted'

    run_as_console_user afplay "$SOUND_FILE"

    sleep "$INTERVAL"
done

Save and run it:

sudo install -m 755 beep-loop.sh /usr/local/bin/beep-loop
sudo /usr/local/bin/beep-loop

Through SSH:

ssh user@mac 'sudo /usr/local/bin/beep-loop'

To leave it running after disconnecting:

ssh -t user@mac 'sudo nohup /usr/local/bin/beep-loop >/var/log/beep-loop.log 2>&1 &'

Stop the background version:

sudo pkill -f /usr/local/bin/beep-loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment