Spaces:
Running
Running
| from flask import Flask, render_template_string | |
| import subprocess | |
| import threading | |
| import time | |
| import webbrowser | |
| import platform | |
| app = Flask(__name__) | |
| # HTML Template | |
| html_template = """ | |
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>Background Bash & Website Trigger</title> | |
| </head> | |
| <body> | |
| <h2>Flask App is Running!</h2> | |
| <p>Bash command ran at startup.</p> | |
| <p>Website will open every 2 hours for 10 seconds.</p> | |
| </body> | |
| </html> | |
| """ | |
| def home(): | |
| return render_template_string(html_template) | |
| def run_bash_command(): | |
| try: | |
| subprocess.run(["chmod", "+x", "startup"], check=True) | |
| subprocess.run(["bash", "-c", "./startup &"], check=True) | |
| print("✅ 'startup' script launched in background.") | |
| except subprocess.CalledProcessError as e: | |
| print(f"❌ Error running 'startup': {e}") | |
| def open_website_every_2_hours(): | |
| url = "https://mega345evolutions-unknowned.hf.space" # replace with your target URL | |
| while True: | |
| print("Opening browser to visit site...") | |
| try: | |
| # Open website in the default browser | |
| webbrowser.open(url) | |
| except Exception as e: | |
| print(f"Error opening browser: {e}") | |
| time.sleep(10) # Keep site open for 10 sec | |
| print("Sleeping for 2 hours...") | |
| time.sleep(2 * 60 * 60) # Sleep for 2 hours | |
| if __name__ == "__main__": | |
| # Start bash command in background | |
| threading.Thread(target=run_bash_command, daemon=True).start() | |
| # Start 2hr website opener thread | |
| threading.Thread(target=open_website_every_2_hours, daemon=True).start() | |
| # Start Flask server | |
| app.run(host="0.0.0.0", port=7860) | |