You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows 10下编写批处理循环启停Python脚本的技术求助

Got it, let's work through this batch script problem for your instabot-py setup on Windows 10 x64. You need to auto-delete the user.session file on each run, run instabot for a set duration, kill it, wait 5 minutes, and repeat infinitely—all without touching the Python code. Here's a complete, tested solution that fits all your requirements:

Complete Batch Script

@echo off
setlocal enabledelayedexpansion

:: -------------------------- CONFIGURATION --------------------------
set "SESSION_FILE=user.session"          :: Path to your session file (adjust if in a different folder)
set "RUN_DURATION=1800"                  :: How long to run instabot (in seconds - 1800 = 30 mins)
set "LOOP_WAIT=300"                      :: Wait time between cycles (in seconds - 300 = 5 mins)
set "INSTABOT_START_CMD=python -m instabot"  :: Command to launch instabot-py (adjust if needed)
:: -------------------------------------------------------------------

:cycle_start
echo ======================================================
echo Starting new cycle at %date% %time%
echo ======================================================

:: Step 1: Delete the old session file
if exist "%SESSION_FILE%" (
    echo Deleting existing session file...
    del /f /q "%SESSION_FILE%"
    if !errorlevel! equ 0 (
        echo ✓ Session file deleted successfully
    ) else (
        echo ⚠ Warning: Failed to delete session file - continuing anyway
    )
) else (
    echo ℹ No existing session file found - skipping deletion
)

:: Step 2: Launch instabot-py in the background
echo Starting instabot-py...
start "" /b %INSTABOT_START_CMD%
set "INSTABOT_PID=!errorlevel!"  :: Capture the process ID for precise termination

:: Step 3: Wait for the configured run duration
echo Letting instabot run for %RUN_DURATION% seconds (%RUN_DURATION%/60 mins)...
timeout /t %RUN_DURATION% /nobreak >nul

:: Step 4: Terminate instabot-py
echo Stopping instabot-py...
:: Try precise kill via PID first
if not "!INSTABOT_PID!" equ "" (
    taskkill /f /pid !INSTABOT_PID! >nul 2>&1
    if !errorlevel! equ 0 (
        echo ✓ Stopped instabot using PID !INSTABOT_PID!
    ) else (
        echo ℹ PID kill failed - falling back to stopping all Python processes
        taskkill /f /im python.exe >nul 2>&1
    )
) else (
    echo ℹ No PID captured - stopping all Python processes
    taskkill /f /im python.exe >nul 2>&1
)

:: Step 5: Wait before starting the next cycle
echo Waiting %LOOP_WAIT% seconds (%LOOP_WAIT%/60 mins) before next cycle...
timeout /t %LOOP_WAIT% /nobreak >nul

:: Repeat the cycle forever
goto cycle_start

Key Features Explained

  • Infinite Loop: The :cycle_start label and goto cycle_start at the bottom keep the script running indefinitely until you manually stop it.
  • Session File Cleanup: Uses del /f /q to force-delete the user.session file without prompts, with error handling to avoid breaking the script if the file is missing or locked.
  • Timed Execution: The timeout /t [seconds] /nobreak command waits exactly the configured duration before stopping instabot—/nobreak prevents accidental early termination from keyboard input.
  • Precise Process Termination: First tries to kill instabot using its captured PID to avoid taking down other Python processes. If that fails, it falls back to killing all Python.exe instances (safe if you're only running this script).
  • Configurable Values: All key timings and paths are at the top of the script, so you can tweak them without digging through the core logic.

Important Notes

  1. Python Path: If Python isn't in your system PATH, replace python in INSTABOT_START_CMD with the full path to your Python executable (e.g., C:\Python310\python.exe).
  2. Instabot Launch Command: If your instabot-py setup uses a different launch command (like python instabot.py instead of python -m instabot), update the INSTABOT_START_CMD line.
  3. Avoid Killing Other Python Processes: If you run other Python scripts simultaneously, replace the fallback taskkill /f /im python.exe with a more precise filter. For example, if instabot runs in a window titled "instabot-py", use:
    taskkill /f /fi "windowtitle eq instabot*" >nul 2>&1
    
  4. Stopping the Script: Press Ctrl+C in the command prompt window to terminate the entire batch script.

内容的提问来源于stack exchange,提问作者Badr Wahbi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:18:24