简体   繁体   English

制作一个批处理文件以运行 python 脚本一段时间,然后自动关闭

[英]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.我需要制作一个批处理文件来删除每次脚本启动时创建的文件,然后设置一个计时器来停止 python 脚本并在一定时间后退出它以运行另一个命令行。 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.问题是 python 程序有点复杂,我无法修改它,所以我所能做的就是创建一个无限期执行此任务的批处理或 powershell 文件。 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.我感到迷茫,我想不出办法,并且必须是一个循环,例如每 5 分钟一次又一次地执行所有这些代码(运行 python 程序 5 分钟然后关闭它并重新开始使用 cd, del, 启动程序并再次停止。 PS:我在 windows 10 x64 上。

You can use powershell jobs for that:您可以为此使用 powershell 作业:

$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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python Flask 脚本在作为 .pyw 文件运行时自动关闭(包括解决方法) - Python Flask script auto-closes when run as .pyw file (workaround included) Selenium 自动关闭 window 打开后 Python - Selenium auto-closes window after opening Python 一定时间后结束python脚本 - End a python script after a certain amount of time 如何在一定时间内执行一个python文件,然后再运行另一个? - How to execute one python file for a certain amount of time and after that, run another? 经过一定时间后如何做出python响应? - How to make python response after a certain amount of time? 我该如何在python中制作一个脚本,该脚本在yy天每隔xx分钟执行一次,并且在时间到期后关闭文件 - How can i make an script in python that executes every xx minutes for yy days and after the time expires it closes the file 如何在python tkinter中使标签出现然后在一定时间后消失 - How to make a Label appear then disappear after a certain amount of time in python tkinter 如何运行在一定时间后返回的搜索? - How to run a search that returns after a certain amount of time? Python屏幕黑屏一段时间,然后关闭 - Python screen is black for a small amount of time then closes selenium 等待一定的时间,然后在 python 中执行脚本 - selenium wait for certain amount of time and then execute script in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM