Created
July 29, 2025 05:42
-
-
Save vishalroygeek/8b92e2a83c5e3a8473376b1f66547fdd to your computer and use it in GitHub Desktop.
A bash script that utilizes Redis MONITOR to count its command execution at defined interval ⌚
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Redis connecting string, interval, command to monitor from CLI | |
| REDIS_CONN=$1 | |
| INTERVAL=$2 | |
| COMMAND=$3 | |
| if [ -z "$REDIS_CONN" ] || [ -z "$INTERVAL" ]; then | |
| echo "Usage: $0 <REDIS_COMMAND> <INTERVAL> <COMMAND_TO_MONITOR>" | |
| echo "Example: $0 'redis-cli -h localhost -p 6379' 10 publish" | |
| exit 1 | |
| fi | |
| echo "Monitoring Redis $COMMAND commands..." | |
| echo "Redis connection: $REDIS_CONN" | |
| echo "Reporting every ${INTERVAL} seconds" | |
| echo "Press Ctrl+C to stop" | |
| echo "" | |
| total_count=0 | |
| interval_count=0 | |
| last_report=$(date +%s) | |
| cleanup() { | |
| echo "Monitoring stopped." | |
| exit 0 | |
| } | |
| trap cleanup INT | |
| # Start to monitor and count commands | |
| $REDIS_CONN monitor | grep -i $COMMAND | while read line; do | |
| total_count=$((total_count + 1)) | |
| interval_count=$((interval_count + 1)) | |
| current_time=$(date +%s) | |
| # Logging status every interval | |
| if [ $((current_time - last_report)) -ge $INTERVAL ]; then | |
| echo "$(date '+%H:%M:%S'): $interval_count $COMMAND commands in last ${INTERVAL}s (Total: $total_count)" | |
| interval_count=0 | |
| last_report=$current_time | |
| fi | |
| done | |
| cleanup |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment