pythonbatch-filewindows-task-scheduler

How to close and rerun a python exe after every 5min using .bat file


I am running a socket code which is written in python and due to some reasons I need to rerun it after every 5 min some one suggested me to use task scheduler I tried, but it was not running so I searched and found a script which runs the program after every 5 min in .exe format. Now main problem is, it opens a new window and old window is still open.

For example, when I run the program it open in 1 number window. After 5 min it opens 2 number window in which program is running but due to it is socket programming it is connected to 1 number window. Now I want to close 1 number window so that the socket may get connect to 2 number window.

I think it is a shell window and program is in .bat format.

The code is:

:loop
@echo off
start python "python program location" 
timeout /t 300 /nobreak
exit0
goto :loop

The line "@echo off" and "exit0" was suggested in some articles but the are not working.

"300" is time in sec of 5 min.


Solution

  • Give the window a title, then kill it before starting the new one. Here we are giving it a title name of MyScript and we're also just killing it by that name:

    @echo off
    :loop
    start "MyScript" python "python program location" 
    timeout /t 300 /nobreak
    taskkill /FI "WINDOWTITLE eq MyScript"
    goto :loop