from flask import Flask, render_template_string from apscheduler.schedulers.background import BackgroundScheduler import subprocess from datetime import datetime import logging from io import StringIO app = Flask(__name__) # In-memory log storage log_buffer = StringIO() log_entries = [] def run_cli_script(): """Execute python cli.py and capture logs""" timestamp = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC") entry = {"time": timestamp, "content": []} try: entry["content"].append(f"{timestamp} - Starting script execution") result = subprocess.run( ["python", "cli.py"], check=True, capture_output=True, text=True ) # Capture stdout if result.stdout: entry["content"].append("Output:\n" + result.stdout) # Capture stderr if result.stderr: entry["content"].append("Errors:\n" + result.stderr) entry["content"].append(f"{timestamp} - Script completed successfully") except subprocess.CalledProcessError as e: entry["content"].append(f"Error: {str(e)}\n" + e.stderr) except Exception as e: entry["content"].append(f"Unexpected error: {str(e)}") finally: log_entries.append(entry) # Keep only last 50 entries if len(log_entries) > 50: log_entries.pop(0) # Configure scheduler scheduler = BackgroundScheduler() scheduler.add_job(run_cli_script, 'interval', hours=3) scheduler.start() @app.route('/') def home(): return render_template_string(''' Script Scheduler

Scheduler Status

Python script runs every 3 hours

Next run: {{ scheduler.next_run_time if scheduler else 'N/A' }}

View Live Logs

''', scheduler=scheduler) @app.route('/logs') def show_logs(): return render_template_string(''' Live Logs

Execution Logs

← Back to Status {% for entry in logs|reverse %}
{{ entry.time }}
{% for line in entry.content %} {{ line }}
{% endfor %}
{% endfor %} ''', logs=log_entries) if __name__ == '__main__': app.run(host='0.0.0.0', port=7860)