VPS Health Check
cat << 'EOF' > vps_check.sh
#!/bin/bash
# Get the VPS IP address dynamically
VPS_IP=$(ip -4 addr show eth0 | grep inet | awk '{print $2}' | cut -d'/' -f1)
echo "Checking status of VPS $VPS_IP"
# Prompt the user to input Telegram Bot Token and Chat ID if not set
if [ -z "$TELEGRAM_BOT_TOKEN" ]; then
read -p "Please enter your Telegram Bot Token: " TELEGRAM_BOT_TOKEN
fi
if [ -z "$TELEGRAM_CHAT_ID" ]; then
read -p "Please enter your Telegram Chat ID: " TELEGRAM_CHAT_ID
fi
# Function to send a message via Telegram
send_telegram_message() {
local message=$1
curl -s -X POST https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendMessage \
-d chat_id=$TELEGRAM_CHAT_ID \
-d text="$message"
}
# Check connection using ping
if ! ping -c 3 $VPS_IP > /dev/null 2>&1; then
MESSAGE="⚠️ VPS at $VPS_IP is not responding!"
send_telegram_message "$MESSAGE"
else
echo "✅ VPS is operating normally."
fi
# Wait for 5 minutes (300 seconds) before checking again
sleep 300
EOF
# Make the script executable
chmod +x vps_check.sh
# Loop to repeatedly check VPS status and log results
while true; do
echo "$(date '+%Y-%m-%d %H:%M:%S') Starting vps_check.sh..."
timeout 10 ./vps_check.sh >> vps_check_output.log 2>&1 # Timeout after 10 seconds
if [ $? -ne 0 ]; then
echo "$(date '+%Y-%m-%d %H:%M:%S') Error: vps_check.sh failed or timed out." >> vps_check_output.log
else
echo "$(date '+%Y-%m-%d %H:%M:%S') vps_check.sh executed successfully."
fi
echo "$(date '+%Y-%m-%d %H:%M:%S') Waiting 1 hour for the next run..."
sleep 3600 # Wait for 1 hour before running again
done
Last updated