Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 291 Vote(s) - 3.57 Average
  • 1
  • 2
  • 3
  • 4
  • 5
WSH / JScript to start and stop services based on time?

#1
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!
Reply

#2
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
Reply

#3
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.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through