[英]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.