views:

16

answers:

1

Hi all,

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!

A: 

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

goober