只是想看看我的评估在最简单的方法上是否正确。
背景:我们有一个软件套件,我们通过服务运行。这些服务需要在晚上10点到早上6点之间关闭。我们还需要每隔10分钟检查一次服务,以防它们在应该启动时停机,以防我们在需要关闭的时候重新启动。
选择:我正在考虑一个每10分钟运行一次WSH / JScript的计划任务。 JScript的pseduo代码类似于:
问题:
预先感谢您提供任何帮助!
答案 0 :(得分:1)
想出来了!
<强> BF_Ping.bat 强>:
D:
cd\
BF_Ping.js
<强> 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;
}
}
我实际上对这个感到有点自豪 - 它第一次没有错误地运行了:)
一些注释:
如果您对我如何改进代码有任何问题或意见,请与我们联系!
谢谢,
- 肖恩
答案 1 :(得分:0)
你提到过:
...(直接指向.js无效,我发现了)
在尝试从Windows 2003中的任务计划程序运行WSH JS文件时,我遇到了这种情况。我在WIn7中发现,只需引用.js文件就可以直接运行它,但它如果在2003 Scheduler中以相同的方式运行,则至少无法将参数传递给脚本。
我确实找到了一种似乎可以避免需要您提到的存根批处理文件的解决方法:如果您只是在.js文件前加上'WScript'(或'CScript') - 则不需要完整路径因为它们在%WINDIR%\ system32中,所以它似乎对我有用,包括.js文件获取任何参数传递。