WSH/JScript 根据时间启动和停止服务?
只是想看看我的评估是否正确,以最简单的方式做到这一点。
背景:我们有一个通过服务运行的软件套件。晚上 10 点至早上 6 点之间需要关闭服务。我们还需要每 10 分钟检查一次以恢复服务,以防服务在应该启动时关闭,以及在服务需要关闭时重新启动。
选择:我正在考虑每 10 分钟运行一次 WSH / JScript 的计划任务。 JScript 的伪代码类似于:
- 获取当前时间
- 如果当前时间是晚上 10 点之后但早上 6 点之前
- 调用 EnsureDowntime()
- 否则
- 调用 EnsureUptime()
- EnsureDownime():
- 如果服务正在运行,请将其停止
- EnsureUptime():
- 如果服务未运行,请启动它们
问题:
- 计划任务/WSH/JScript 是执行此操作的最佳方法,或者至少是可接受的方法吗?
- 调用 new Date() 并使用 .getHours() 是查找时间的最佳方式吗?
- 我的伪代码看起来是解决这个问题的最佳方法吗?
- 如何检测服务的状态(运行/非运行)?
- 或者在这种情况下,无论状态如何,我都应该启动/停止服务吗?如果没有错误并且它不会重新启动已启动的服务,我想我可以只使用“net start”和“net stop”。
预先感谢您提供的任何帮助!
Just looking to see if my assessment is correct on the easiest way to do this.
Background: we have a software suite which we're running via services. The services need to be shut down between 10pm and 6am. We also need to check every 10 min to recover the services in case they've gone down when they were supposed to be up, and in case we restarted during a time when they'd need to be shut down.
Choices: I'm thinking a scheduled task that runs a WSH / JScript every 10 minutes. The pseduo code for the JScript would be something like:
- Get current time
- If the current time is after 10pm but before 6am
- call EnsureDowntime()
- Else
- call EnsureUptime()
- EnsureDownime():
- If the services are running, stop them
- EnsureUptime():
- If the services are not running, start them
Questions:
- Is Scheduled Task / WSH / JScript the best way to do this, or at least an acceptable way?
- is calling a new Date() and using .getHours() the best why to find the time?
- Does my pseudo-code look like the best way to approach this?
- how can I detect the state of a service (running / non-running)?
- or in this case, should I just start / stop the services regardless of state? If there are no errors and it won't re-start a service that's already started, I imagine I could just use "net start" and "net stop".
Thanks in advance for any help you can give!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
想通了!
解决方案演练
BF_Ping.bat:
BF_Ping.js:
我实际上对此感到有点自豪 - 它第一次运行时没有错误:)
一些注意事项:
如果您对我如何改进代码有任何问题或意见,请告诉我!
谢谢,
-
肖恩
Figured it out!
Solution Walkthrough
BF_Ping.bat:
BF_Ping.js:
I'm actually a little proud of this one -- it ran without error the first time :)
Some Notes:
Please let me know if you have any questions or comments on how I might improve the code!
Thanks,
--
Sean
你提到:
这也是我在 Windows 2003 中尝试从任务计划程序运行 WSH JS 文件时遇到的问题。我在 WIn7 中发现,您可以只需引用 .js 文件即可直接运行它,但如果在 2003 Scheduler 中以相同方式运行,它至少无法将参数传递给脚本。
我确实找到了一种解决方法,似乎可以避免需要您提到的存根批处理文件:如果您只是在 .js 文件中添加“WScript”(或“CScript”)前缀 - 则不需要完整路径,因为这些是在 %WINDIR%\system32 中,那么它似乎对我有用,包括传递任何参数的 .js 文件。
You mentioned that:
That was something I'd run into as well when trying to run a WSH JS file from Task Scheduler in Windows 2003. I found in WIn7, you could run it directly with just a reference to the .js file, but it failed to pass parameters at least to the script if run the same way in the 2003 Scheduler.
I did find a workaround that seems to work to avoid needing the stub batch file that you mentioned though: if you just prefix the .js file with 'WScript' (or 'CScript') -- no full path should be needed because those are in %WINDIR%\system32, then it seems to work for me including the .js file getting any parameters passed.