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_startlabel andgoto cycle_startat the bottom keep the script running indefinitely until you manually stop it. - Session File Cleanup: Uses
del /f /qto force-delete theuser.sessionfile without prompts, with error handling to avoid breaking the script if the file is missing or locked. - Timed Execution: The
timeout /t [seconds] /nobreakcommand waits exactly the configured duration before stopping instabot—/nobreakprevents 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
- Python Path: If Python isn't in your system PATH, replace
pythoninINSTABOT_START_CMDwith the full path to your Python executable (e.g.,C:\Python310\python.exe). - Instabot Launch Command: If your instabot-py setup uses a different launch command (like
python instabot.pyinstead ofpython -m instabot), update theINSTABOT_START_CMDline. - Avoid Killing Other Python Processes: If you run other Python scripts simultaneously, replace the fallback
taskkill /f /im python.exewith 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 - Stopping the Script: Press
Ctrl+Cin the command prompt window to terminate the entire batch script.
内容的提问来源于stack exchange,提问作者Badr Wahbi
相关产品推荐
相关产品推荐

