Spaces:
Paused
Paused
File size: 3,392 Bytes
57e8d93 d08a4b7 57e8d93 d08a4b7 57e8d93 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | #!/bin/bash
set -e
echo "π¦ Installing Python dependencies..."
# Removed --break-system-packages (not supported on Debian 11 / Python 3.9)
pip install requests
# --- GENERATE server.py (Foreground Web Server for HF Health Check) ---
cat > /app/server.py << 'PYEOF'
from http.server import HTTPServer, BaseHTTPRequestHandler
from datetime import datetime
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
html = f"""
<html><body style="font-family:sans-serif;text-align:center;padding-top:50px;">
<h1>β€οΈ Running</h1>
<p>Space active. Keep-alive & SSHX tunnel operational.</p>
<small>{datetime.now().strftime("%Y-%m-%d %H:%M:%S")}</small>
</body></html>"""
self.wfile.write(html.encode())
def log_message(self, format, *args):
pass
if __name__ == "__main__":
print("β
Web server running on port 7860")
HTTPServer(("0.0.0.0", 7860), Handler).serve_forever()
PYEOF
# --- GENERATE keep_alive.py (Background Randomized Pinger) ---
cat > /app/keep_alive.py << 'PYEOF'
import requests, time, random
from datetime import datetime, timedelta
SPACE_URL = "https://awesome-developer-meow.hf.space"
MIN_HOURS, MAX_HOURS = 20, 46 # Must be < 48h to prevent pause
LOG_FILE = "/data/ping.log"
def log_ping(status, msg=""):
ts = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
try:
with open(LOG_FILE, "a") as f:
f.write(f"[{ts}] {status} {msg}\n")
except Exception:
pass
def ping_space():
try:
r = requests.get(SPACE_URL, timeout=30)
if r.status_code == 200:
log_ping("SUCCESS", f"Status: {r.status_code}")
return True
else:
log_ping("ERROR", f"Unexpected status: {r.status_code}")
return False
except Exception as e:
log_ping("ERROR", str(e))
return False
def main():
log_ping("STARTUP", "Keep-alive initialized")
ping_space()
while True:
hours = random.uniform(MIN_HOURS, MAX_HOURS)
sleep_secs = int(hours * 3600)
next_time = datetime.now() + timedelta(seconds=sleep_secs)
log_ping("SLEEPING", f"Next in {hours:.2f}h at {next_time.strftime('%H:%M:%S')}")
time.sleep(sleep_secs)
ping_space()
if __name__ == "__main__":
main()
PYEOF
# Start SSH service (required for sshx)
service ssh start 2>/dev/null || true
# --- START SSHX AND CAPTURE URL ---
echo " Starting sshx tunnel..."
nohup sshx > /tmp/sshx_output.log 2>&1 &
SSHX_PID=$!
disown $SSHX_PID
for i in {1..15}; do
if grep -q "sshx.io" /tmp/sshx_output.log; then
SSHX_URL=$(grep -oP 'https://sshx\.io/[^\s]+' /tmp/sshx_output.log | head -1)
echo "$SSHX_URL" > /data/sshx_url.txt
echo "β
SSHX URL saved to /data/sshx_url.txt: $SSHX_URL"
break
fi
sleep 1
done
if [ ! -f /data/sshx_url.txt ]; then
echo "β οΈ SSHX did not connect within 15s. Check /tmp/sshx_output.log"
fi
# --- START SILENT BACKGROUND KEEP-ALIVE PINGER ---
echo "π Starting silent background keep-alive..."
nohup python3 /app/keep_alive.py > /dev/null 2>&1 &
disown
# --- START FOREGROUND WEB SERVER (HF HEALTH CHECK) ---
echo "π Starting main web server on :7860..."
exec python3 /app/server.py |