简体   繁体   中英

Make a batch file to run python script for a certain amount of time and then auto-closes after

I need to make a batch file to delete a file created each time a script starts, then, set a timer to stop the python script and exit it to run another command line after a certain amount of time. Here's the code, I can't find any solution. The thing is that the python program is a bit complicated and I can't modify it so all I can do is create a batch or powershell file that does this task indefinitely. Here's the batch file:

cd C:\Users\Admin\Desktop\instabot.py-master
del -f user.session
instabot-py @user
#Here I need a line to stop the instabot-py running on cmd maybe using a condition before launching the script

I feel lost I can't figure out a way to do that, and the must would be a loop that does all this code again and again every 5 minutes for example (runs the python program for 5 minutes then closes it and start again at with cd, del, launches the program and stops it again. PS: I'm on windows 10 x64.

You can use powershell jobs for that:

$myScript = {
    cd c:\some\folder
    python .\py_script.py
}

$timeOut = 10

while ($true){
    $job = start-job -ScriptBlock $myScript
    Start-Sleep $timeOut
    Remove-Item -Path .\some.file
    stop-job -Job $job 
    Remove-Job -Job $job
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM