英文:
How can I modify my Continuous Ping batch to write to a new file daily as a Scheduled Task?
问题
如何修改下面的代码,使其在每天午夜后写入一个新的带日期的文件?
set host="8.8.8.8"
set curdate="%date:~-10,2%-%date:~-7,2%-%date:~-2,2%"
set filep="c:\ping\ping-%curdate%.txt"
ping -t %host% | cmd /q /v /c "(pause&pause)>nul & for /l %%a in () do (set /p "data=" && echo(!date! !time! !data! -)&ping -n 2 %host%>nul">>%filep%
如果我能在午夜前终止代码运行,使用类似"if time=11:59, then stop script"的条件,那应该能解决我的问题。任务计划程序只有指定的值,如12小时或1天。
(更新于2023年5月31日)我已经测试了许多方法,并在答案中发布了一个解决方法。
英文:
How can I modify the code below to have it write to a new dated file after midnight every day?
set host="8.8.8.8"
set curdate="%date:~-10,2%-%date:~-7,2%-%date:~-2,2%"
set filep="c:\ping\ping-%curdate%.txt"
ping -t %host% | cmd /q /v /c "(pause&pause)>nul & for /l %%a in () do (set /p "data=" && echo(!date! !time! !data! -)&ping -n 2 %host%>nul">>%filep%
If I can get the code to terminate prior to midnight, using some sort of "if time=11:59, then stop script", that should solve my problem. Task Scheduler only has specified values like 12 hours, or 1 day.
(update 5-31-23) I have tested numerous things and posted a work-around as an answer.
答案1
得分: 0
以下是翻译好的内容:
这是一个有趣的问题,昨晚花了一个多小时试图在脚本中找出解决方案。我认为问题在于,虽然 cmd /v
启用了延迟环境变量扩展,这允许你将日期和时间输出到文件保持最新,但这并不适用于你将结果导出到的文件名,我想不出解决这个问题的办法。
不幸的是,我怀疑唯一可行的解决方法(除非有人提出更好的想法)是使用任务计划程序来处理每天停止和重新启动脚本。
创建任务,并在触发器中将其设置为每天运行 00:00:00
。
在设置中,确保选中“允许按需运行任务”(以便在需要时可以手动启动它),然后在底部将“不启动新实例”更改为“停止现有实例”。
这个组合应该意味着你可以在任何时间启动脚本,然后在午夜时,脚本的新实例将按计划启动,这将反过来停止先前运行的实例。由于现在是脚本的新实例,它将获取新的日期,因此将结果输出到带有当前日期的新文件中。
如果需要,你还可以使用 schtasks
通过 cmd
或其他脚本来启动任务。但我没有使用过它,所以无法评论它的实际工作情况。
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/schtasks-run
英文:
It's an interesting question, spent over an hour last night trying to come up with a solution within the script. I think the issue is that while cmd /v
enables delayed environment variable expansion, and that allows the date + time you're outputting to file to remain current, that doesn't apply to the filename you're piping the results to, and I can't think of a way around that.
Unfortunatly I suspect the only work around that will do it (unless someone comes up with a better idea) is to use Task Scheduler to handle stopping and restarting the script each day.
Create the task and in Triggers set it to run Daily at 00:00:00
In Settings, make sure Allow task to be run on demand
is ticked (so you can manually start it when required), then at the bottom change Do not start a new instance
to be Stop the existing instance
.
That combination should mean that you can start the script whenever you want, then at midnight a new instance of the script will be started on schedule which in turn stops the previously running instance. Since it's now a new instance of the script it will pick up the new date, and therefore output the results to a new file with the current date.
If you want you might also be able to use schtasks
to initiate the task via cmd
or another script if needed. But I've not used it so can't comment on how well it actually works.
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/schtasks-run
答案2
得分: 0
尽管我还没有找到基于时间的批处理终止方式(在晚上11:59终止),但我成功地找到了一个解决方法,对我来说已经足够好了。
我对第一个ping命令进行了修改。不使用"ping -t",而是使用"ping -w 2000 -n 86400"。这样只等待2秒以进行失败判断,并且ping 86400次(相当于1天的秒数,如果整天都能ping通,可能会稍少一些,如果请求超时,则可能稍多,但足够接近)。
接下来,任务计划程序似乎无法在所有结束/停止任务后正确结束任务,即使已设置所有在1天后结束任务的选项。为了解决这个问题,除了ping的“计时器”之外,在任务计划程序的最后一个选项卡中,底部的下拉菜单,将其设置为“并行运行另一个实例”。这将始终在12:00:05 am启动脚本,即使批处理正在运行,然后前一天的批处理将在ping运行完之后停止。
set host="8.8.8.8"
set curdate="%date:~-10,2%-%date:~-7,2%-%date:~-2,2%"
set filep="c:\ping\ping-%curdate%.txt"
ping -w 2000 -n 86400 %host% | cmd /q /v /c "(pause&pause)>nul & for /l %%a in () do (set /p "data=" && echo(!date! !time! !data! -)&ping -n 2 %host%>nul">>%filep%
我将不得不在其他操作系统和其他计算机上测试这个功能,以确保它在各个平台上的表现相同,但我相信它目前将能够完成我需要的任务。(仍然想知道是否可能实现基于时间的终止)。
英文:
Although I haven't found a time-based termination of the batch (at 11:59pm, terminate), I did manage to do a work-around that will be good enough for me.
I made modifications to the first ping command. Instead of "ping -t" I did "ping -w 2000 -n 86400". This makes it only wait 2 seconds for failure, and ping for 86400 times (1 day's worth of seconds, may get slightly less if it pings well all day, or slightly more if request timed out, but close enough).
Next, the Task Scheduler doesn't seem to properly end the task even with all end/stop task after 1 day things are set. To get around this, along with the "timer" on the ping, on the last tab of task scheduler, the dropdown at the bottom, set it to "Run another instance in parallel". This will always start the script at 12:00:05am even if a batch is running, then the previous day's batch will stop after the pings run out.
set host="8.8.8.8"
set curdate="%date:~-10,2%-%date:~-7,2%-%date:~-2,2%"
set filep="c:\ping\ping-%curdate%.txt"
ping -w 2000 -n 86400 %host% | cmd /q /v /c "(pause&pause)>nul & for /l %%a in () do (set /p "data=" && echo(!date! !time! !data! -)&ping -n 2 %host%>nul">>%filep%
I will have to test this on other operating systems and other computers to ensure it functions the same across the board, but I believe it will accomplish what I need it to for now. (still interested to know if time-based termination is possible)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论