Last active
February 26, 2024 22:09
-
-
Save groundcat/625de64751a99114189f0c683c694185 to your computer and use it in GitHub Desktop.
Auto restart the VPS through the Virtualizor end-user API when the VPS is down
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 | |
# Define variables | |
VPS_IP="" | |
API_KEY="" | |
API_PASSWORD="" | |
PANEL_URL="https://panel.example.com:4083" | |
# Stop the VPS | |
stop_vps() { | |
curl -k -L "${PANEL_URL}/index.php?svs=375&act=stop&api=json&apikey=${API_KEY}&apipass=${API_PASSWORD}&do=1" | |
} | |
# Start the VPS | |
start_vps() { | |
curl -k -L "${PANEL_URL}/index.php?svs=375&act=start&api=json&apikey=${API_KEY}&apipass=${API_PASSWORD}&do=1" | |
} | |
# Ping the VPS IP | |
for i in {1..3}; do | |
echo "Attempt $i: Pinging ${VPS_IP}..." | |
ping -c 1 -W 5 $VPS_IP &> /dev/null | |
if [ $? -eq 0 ]; then | |
echo "No problem." | |
exit 0 | |
else | |
echo "Ping failed, no response after 5 seconds." | |
fi | |
if [ $i -eq 3 ]; then | |
echo "Max retries reached. Stopping and starting the VPS..." | |
stop_vps_output=$(stop_vps) | |
echo "Stop VPS output: $stop_vps_output" | |
sleep 5 # wait for a bit before starting the VPS | |
start_vps_output=$(start_vps) | |
echo "Start VPS output: $start_vps_output" | |
sleep 120 | |
fi | |
sleep 5 # Wait a bit before retrying | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment