WSH/JScript 根据时间启动和停止服务?

发布于 2024-09-27 19:02:02 字数 843 浏览 0 评论 0原文

只是想看看我的评估是否正确,以最简单的方式做到这一点。

背景:我们有一个通过服务运行的软件套件。晚上 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

惟欲睡 2024-10-04 19:02:02

想通了!

解决方案演练

  • 首先,创建一个计划任务并将其指向一个 .bat 文件(我发现直接将其指向 .js 是行不通的)。
  • 在批处理文件中指向您的 .js 文件来运行它(例如,我的文件位于“D:\BF_Ping.js”

BF_Ping.bat

D:
cd\
BF_Ping.js
  • 接下来,创建您的 JavaScript 文件。我的完整评论文件如下供参考:

BF_Ping.js:

//*** Time Variables ***//
var UptimeBeginsAtHour = 6;     // Hour at which the services should be started
var UptimeEndsAtHour = 22;      // Hour at which the services should be ended

//*** Flags ***//
var KILL_FLAG = FindFile("C:\\BF_KILL.txt"); // If this flag is true, services will be shut down and not started.
var LAZY_FLAG = FindFile("C:\\BF_LAZY.txt"); // If this flag is true, nothing in the script will run.

if (KILL_FLAG == true)
{
    StopBizFlowServices();
}

if (KILL_FLAG == false && LAZY_FLAG == false)
{
    DetectTimeAndProcess(UptimeBeginsAtHour, UptimeEndsAtHour);
}

/***
 * DetectTimeAndProcess(startAtHour, stopAtHour):
 * Starts or stops BizFlow Services based on uptime variables.
 * 
 * ---Arguments---
 * startAtHour  Hour after which services should be started (defined by variable in main code)
 * stopAtHour   Hour after which services should be started (defined by variable in main code)
 * 
 * ---Returns---
 * None (void)
 * 
 * ---Called By---
 * Main code.
 */

function DetectTimeAndProcess(startAtHour, stopAtHour)
{
    var currentTime = new Date();   
    var hour = currentTime.getHours();      // Returns Hour in 24-hour format

    if (hour > startAtHour && hour < stopAtHour)
    {
        StartBizFlowServices();
    }
    else
    {
        StopBizFlowServices();
    }
}

/***
 * StartBizFlowServices():
 * Starts BizFlow Services using "net start" and the service name.
 * 
 * --- Arguments ---
 * None.
 * 
 * --- Returns ---
 * None (void)
 * 
 * --- Called By---
 * DetectTimeAndProcess()
 */
function StartBizFlowServices()
{
    var objShell = WScript.CreateObject("WScript.Shell");

    objShell.Run("net start \"BizFlow Database Service\" ", 1, true);
    objShell.Run("net start \"BizFlow Main Service\"", 1, true);
    objShell.Run("net start \"BizFlow OLE-DB Service\"", 1, true);
    objShell.Run("net start \"BizFlow PKI Service\"", 1, true);
    objShell.Run("net start \"BizFlow Queue Service\"", 1, true);
    objShell.Run("net start \"BizFlow Scheduler Service\"", 1, true);
}

/***
 * StopBizFlowServices():
 * Stops BizFlow Services using "net stop" and the service name.
 * 
 * --- Arguments ---
 * None.
 * 
 * --- Returns ---
 * None (void)
 * 
 * --- Called By---
 * DetectTimeAndProcess()
 */
function StopBizFlowServices()
{
    var objShell = WScript.CreateObject("WScript.Shell");

    objShell.Run("net stop \"BizFlow OLE-DB Service\"", 1, true);
    objShell.Run("net stop \"BizFlow PKI Service\"", 1, true);
    objShell.Run("net stop \"BizFlow Queue Service\"", 1, true);
    objShell.Run("net stop \"BizFlow Scheduler Service\"", 1, true);
    objShell.Run("net stop \"BizFlow Main Service\"", 1, true);
    objShell.Run("net stop \"BizFlow Database Service\"", 1, true);

}

/***
 *
 * FindFile (filePath):
 * Searches for the existence of a given file path.
 * 
 * --- Arguments ---
 * filePath     Full Path of file (including file name)
 * 
 * --- Returns ---
 * true     if file is found
 * false    if file is not found
 * 
 * --- Called By---
 * Main Code (while setting flags)
 */

function FindFile(filePath)
{

    var fso;  //For FileSystemObject
    fso = new ActiveXObject("Scripting.FileSystemObject");

    if(fso.FileExists(filePath))
    {
        return true;
    }
    else
    {
        return false;
    }
}

我实际上对此感到有点自豪 - 它第一次运行时没有错误:)

一些注意事项:

  • 时间是以 24 小时格式给出。
  • 将计划任务设置为每 5-10 分钟运行一次,以确保经常执行检查。
  • 我在这里使用的服务是 BizFlow 服务,这是客户正在使用的产品。当然,您可以相应地更改函数的名称。
  • 运行服务名称的“net start”或“net stop”时,请注意引号的转义字符。
  • 使用 services.msc 中显示的完整服务名称。

如果您对我如何改进代码有任何问题或意见,请告诉我!

谢谢,

-
肖恩

Figured it out!

Solution Walkthrough

  • Firstly, create a scheduled task and point it to a .bat file (pointing it directly to a .js will not work, I discovered).
  • In the batch file point to your .js file to run it (for example, mine was located in "D:\BF_Ping.js"

BF_Ping.bat:

D:
cd\
BF_Ping.js
  • Next, create your JavaScript file. My fully commented file is below for reference:

BF_Ping.js:

//*** Time Variables ***//
var UptimeBeginsAtHour = 6;     // Hour at which the services should be started
var UptimeEndsAtHour = 22;      // Hour at which the services should be ended

//*** Flags ***//
var KILL_FLAG = FindFile("C:\\BF_KILL.txt"); // If this flag is true, services will be shut down and not started.
var LAZY_FLAG = FindFile("C:\\BF_LAZY.txt"); // If this flag is true, nothing in the script will run.

if (KILL_FLAG == true)
{
    StopBizFlowServices();
}

if (KILL_FLAG == false && LAZY_FLAG == false)
{
    DetectTimeAndProcess(UptimeBeginsAtHour, UptimeEndsAtHour);
}

/***
 * DetectTimeAndProcess(startAtHour, stopAtHour):
 * Starts or stops BizFlow Services based on uptime variables.
 * 
 * ---Arguments---
 * startAtHour  Hour after which services should be started (defined by variable in main code)
 * stopAtHour   Hour after which services should be started (defined by variable in main code)
 * 
 * ---Returns---
 * None (void)
 * 
 * ---Called By---
 * Main code.
 */

function DetectTimeAndProcess(startAtHour, stopAtHour)
{
    var currentTime = new Date();   
    var hour = currentTime.getHours();      // Returns Hour in 24-hour format

    if (hour > startAtHour && hour < stopAtHour)
    {
        StartBizFlowServices();
    }
    else
    {
        StopBizFlowServices();
    }
}

/***
 * StartBizFlowServices():
 * Starts BizFlow Services using "net start" and the service name.
 * 
 * --- Arguments ---
 * None.
 * 
 * --- Returns ---
 * None (void)
 * 
 * --- Called By---
 * DetectTimeAndProcess()
 */
function StartBizFlowServices()
{
    var objShell = WScript.CreateObject("WScript.Shell");

    objShell.Run("net start \"BizFlow Database Service\" ", 1, true);
    objShell.Run("net start \"BizFlow Main Service\"", 1, true);
    objShell.Run("net start \"BizFlow OLE-DB Service\"", 1, true);
    objShell.Run("net start \"BizFlow PKI Service\"", 1, true);
    objShell.Run("net start \"BizFlow Queue Service\"", 1, true);
    objShell.Run("net start \"BizFlow Scheduler Service\"", 1, true);
}

/***
 * StopBizFlowServices():
 * Stops BizFlow Services using "net stop" and the service name.
 * 
 * --- Arguments ---
 * None.
 * 
 * --- Returns ---
 * None (void)
 * 
 * --- Called By---
 * DetectTimeAndProcess()
 */
function StopBizFlowServices()
{
    var objShell = WScript.CreateObject("WScript.Shell");

    objShell.Run("net stop \"BizFlow OLE-DB Service\"", 1, true);
    objShell.Run("net stop \"BizFlow PKI Service\"", 1, true);
    objShell.Run("net stop \"BizFlow Queue Service\"", 1, true);
    objShell.Run("net stop \"BizFlow Scheduler Service\"", 1, true);
    objShell.Run("net stop \"BizFlow Main Service\"", 1, true);
    objShell.Run("net stop \"BizFlow Database Service\"", 1, true);

}

/***
 *
 * FindFile (filePath):
 * Searches for the existence of a given file path.
 * 
 * --- Arguments ---
 * filePath     Full Path of file (including file name)
 * 
 * --- Returns ---
 * true     if file is found
 * false    if file is not found
 * 
 * --- Called By---
 * Main Code (while setting flags)
 */

function FindFile(filePath)
{

    var fso;  //For FileSystemObject
    fso = new ActiveXObject("Scripting.FileSystemObject");

    if(fso.FileExists(filePath))
    {
        return true;
    }
    else
    {
        return false;
    }
}

I'm actually a little proud of this one -- it ran without error the first time :)

Some Notes:

  • Times are given in 24 hour format.
  • Set the scheduled task to run every 5-10 minutes to ensure that the check is performed often.
  • The services I use here are BizFlow services, a product that a client is using. You can change the names of the functions accordingly, of course.
  • Note the escape characters for the quotes when running "net start" or "net stop" for the service name.
  • Use the full service name, as it appears in services.msc.

Please let me know if you have any questions or comments on how I might improve the code!

Thanks,

--
Sean

想你的星星会说话 2024-10-04 19:02:02

你提到:

...(我发现直接将其指向 .js 是行不通的)

这也是我在 Windows 2003 中尝试从任务计划程序运行 WSH JS 文件时遇到的问题。我在 WIn7 中发现,您可以只需引用 .js 文件即可直接运行它,但如果在 2003 Scheduler 中以相同方式运行,它至少无法将参数传递给脚本。

我确实找到了一种解决方法,似乎可以避免需要您提到的存根批处理文件:如果您只是在 .js 文件中添加“WScript”(或“CScript”)前缀 - 则不需要完整路径,因为这些是在 %WINDIR%\system32 中,那么它似乎对我有用,包括传递任何参数的 .js 文件。

You mentioned that:

... (pointing it directly to a .js will not work, I discovered)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文