T

T

T

T

T

import os
import time
import logging
import psutil
from datetime import datetime

# ----------------------- CONFIG -----------------------

TASK_NAME = "RVN-Minrt-Task"  # Exact name of your scheduled task
BATCH_PATH = r"C:\Users\Mining Rig\Desktop\T-RexMiner\RVN-ravenminer.bat"
CMD_PROCESS = "cmd.exe"
CHECK_INTERVAL = 60  # seconds
START_TIME = "06:00"
END_TIME = "21:00"
LOG_FILE = "rvn_launcher.log"

# ---------------------- LOGGING -----------------------

logging.basicConfig(
    filename=LOG_FILE,
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s'
)

# -------------------- FUNCTIONS -----------------------

def is_batch_running(batch_path):
    """Check if a cmd.exe process is running the batch file."""
    for proc in psutil.process_iter(['name', 'cmdline']):
        try:
            name = proc.info.get('name', '').lower()
            cmdline = ' '.join(proc.info.get('cmdline') or []).lower()
            if name == CMD_PROCESS and batch_path.lower() in cmdline:
                return True
        except Exception:
            continue
    return False

def kill_batch_processes(batch_path):
    """Kill all cmd.exe processes running the target .bat file."""
    for proc in psutil.process_iter(['name', 'cmdline', 'pid']):
        try:
            name = proc.info.get('name', '').lower()
            cmdline = ' '.join(proc.info.get('cmdline') or []).lower()
            if name == CMD_PROCESS and batch_path.lower() in cmdline:
                proc.kill()
                logging.info(f"Killed cmd.exe running {batch_path} (PID {proc.pid})")
        except Exception as e:
            logging.error(f"Error killing cmd.exe: {e}")

def run_task(task_name):
    """Run Windows Task Scheduler task by name."""
    try:
        result = os.system(f'schtasks /run /tn "{task_name}"')
        if result == 0:
            logging.info(f"Scheduled task '{task_name}' started.")
        else:
            logging.error(f"Failed to start scheduled task '{task_name}' (code {result}).")
    except Exception as e:
        logging.error(f"Error running task: {e}")

# ------------------- INITIAL CHECK --------------------

logging.info("Script started. Waiting for scheduled time...")

now = datetime.now().strftime("%H:%M")
if now < START_TIME or now >= END_TIME:
    if is_batch_running(BATCH_PATH):
        logging.info("Outside allowed hours. Killing running batch process.")
        kill_batch_processes(BATCH_PATH)
else:
    logging.info("Within allowed time, script will manage state based on time loop.")

# -------------------- MAIN LOOP -----------------------

while True:
    try:
        now = datetime.now().strftime("%H:%M")
        logging.info(f"Time check: {now}")

        if now == START_TIME and not is_batch_running(BATCH_PATH):
            logging.info("Start time match. Launching task.")
            run_task(TASK_NAME)

        elif now == END_TIME and is_batch_running(BATCH_PATH):
            logging.info("End time match. Killing process.")
            kill_batch_processes(BATCH_PATH)

        time.sleep(CHECK_INTERVAL)

    except Exception as e:
        logging.error(f"Unexpected error: {e}")
        time.sleep(CHECK_INTERVAL)

Nach oben scrollen
Datenschutz-Übersicht

Diese Website verwendet Cookies, damit wir dir die bestmögliche Benutzererfahrung bieten können. Cookie-Informationen werden in deinem Browser gespeichert und führen Funktionen aus, wie das Wiedererkennen von dir, wenn du auf unsere Website zurückkehrst, und hilft unserem Team zu verstehen, welche Abschnitte der Website für dich am interessantesten und nützlichsten sind.