When monitoring remote servers via SSH, traditional alert mechanisms like email notifications can be overkill for simple events. Many admins want instant, unobtrusive feedback through their local machine's audio system when specific conditions occur on the server.
Surprisingly, OpenSSH has built-in support for forwarding beep commands through the escape character protocol:
#!/bin/bash # On the remote server: echo -e '\a' > /dev/console # Traditional beep # Or using proper SSH escape sequence: echo -e '\033[?5h\033[?5l' | ssh user@localhost
1. Using SSH LocalCommand
Add this to your ~/.ssh/config:
Host myserver HostName server.example.com LocalCommand printf '\a' PermitLocalCommand yes
Then trigger with:
ssh myserver 'your_command && touch ~/.beep_trigger'
2. Named Pipe Notification
Create a named pipe on your local machine:
mkfifo ~/.alert_pipe
Run this in your terminal before connecting:
while true; do beep < ~/.alert_pipe; done
Then from remote:
echo "1" | ssh user@localmachine "cat > ~/.alert_pipe"
3. Simple Socket Approach
Local listener (Python):
import socket s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.bind(('localhost', 6789)) while True: data, addr = s.recvfrom(1024) print('\a', end='', flush=True)
Remote trigger:
echo "beep" | nc -u localhost 6789
This Python script works on both Linux and macOS:
# beep_forwarder.py import sys if sys.platform == 'darwin': import os def beep(): os.system('echo -e "\a"') else: import winsound def beep(): winsound.Beep(1000, 200) beep()
Call it remotely via SSH:
ssh user@host "python3 -c 'import sys; sys.path.append(\"/tmp\"); from beep_forwarder import beep; beep()'"
When implementing these solutions:
- Use temporary files with strict permissions rather than named pipes in production
- For socket solutions, bind to 127.0.0.1 only
- Consider using SSH certificates for authentication
- Add rate limiting to prevent beep flooding
Here's how to integrate with common monitoring tools:
For cron jobs:
* * * * * /path/to/script.sh && ssh user@localmachine "beep"
With inotifywait:
inotifywait -m /path/to/watch -e create | while read; do ssh user@localmachine "beep" done
For systemd services:
[Unit] Description=Beep on service failure [Service] ExecStart=/usr/bin/your_service ExecStartPost=/usr/bin/ssh user@localmachine "beep"
When monitoring remote servers via SSH, we often need immediate feedback for critical events. While solutions like email alerts exist, they're overkill for quick local notifications. The fundamental issue is that commands like beep
execute on the remote server's hardware, not your local machine.
SSH actually has built-in support for triggering local alerts through escape character sequences. Try this in your SSH session:
echo -e '\a' # Standard ASCII bell character
echo -e '\007' # Octal representation
However, this requires:
- Local terminal emulator support
- Enabled bell notifications in terminal preferences
- Working sound system on your local machine
Here's how to implement this in a monitoring script:
#!/bin/bash
# monitor_disk.sh
THRESHOLD=90
DISK_USAGE=$(df -h / | awk 'NR==2 {print $5}' | tr -d '%')
if [ $DISK_USAGE -gt $THRESHOLD ]; then
echo -e "Warning: Disk usage at ${DISK_USAGE}%\a" >&2
# Multiple beeps for urgent alerts
for i in {1..3}; do
echo -e '\a'
sleep 0.5
done
fi
For more reliable cross-platform beeps, create a notification tunnel:
# On local machine (before SSH):
nc -l 12345 | while read; do echo -e '\a'; done
# In remote script:
echo "ALERT" | nc localhost 12345
Linux/OSX: Use afplay
(OSX) or paplay
(Linux) with sound files
# Remote command:
ssh user@host 'echo "event occurred" | mail -s "Alert" localuser@localhost'
# Local .forward file:
| if [ "$USER" = "localuser" ]; then /usr/bin/afplay /System/Library/Sounds/Ping.aiff; fi
Windows: Use PowerShell's [System.Media.SystemSounds]::Beep.Play()
- Check
ssh -v
for connection debugging - Test with
echo -e '\a'
in local terminal first - Configure terminal emulator to enable visual bell if audio fails
- For persistent connections, consider
tmux
orscreen
with bell monitoring