Solved

Need to be notified if scheduled task runs too long.

Posted on 2010-09-17
31
856 Views
Last Modified: 2012-05-10
I have a number of scheduled tasks that run several times a day and usually complete is less thsn 5 min. I need to be notified, by email preferably, when a task runs for more wgan 30 min. C an this be done?
0
Comment
Question by:jimmylew52
  • 14
  • 13
  • 2
  • +1
31 Comments
 
LVL 12

Expert Comment

by:FDiskWizard
ID: 33704379
Hmm, tricky... a couple of ways maybe.
If you're using a custom script you could setup something within it.

But if just running an app, then...
Maybe you could setup a 2nd task that just checks for the process of the 1st task, and sends an alert if its running. Sending an email is easy via something like BMAIL.exe
Or maybe better:
You could have your command running from a .bat file, and at the end of the batch have it write a file.
have it delete the file when starting.... So it doesn't exist until it completes.

Have the 2nd start 30mins later and check for the file:
 IF EXISTS Filename.txt {SendEmail..}

 
0
 
LVL 26

Expert Comment

by:pony10us
ID: 33704468
I agree that this is not an easy task but you could take a look at:  http://www.codeproject.com/KB/install/RunAndWait.aspx

Also, you could possible work up something to monitor the log:

The Task Scheduler writes its status information to a log file called SchedLogU.txt. This file is located in the %WINDOWSDIR%\Tasks directory.

IMPORTANT NOTE: The %WINDOWSDIR%\Tasks is a hidden system directory, and is not listed by the Windows Explorer by default.
 (Taken from:  http://www.activexperts.com/support/activmonitor/online/scheduledtasks/)
0
 
LVL 11

Expert Comment

by:Ben Personick
ID: 33705107
You can do this to get the time a task started running note I have this set to only return a value if the task actually has a status of "Running"

using this value you can calculate the time the task has been running, below I have the code only output to screen the information you'll be interested in.


a more advanced script would loop through a list of tasks names you provide, and if it is running check the start time, add X minutes, and see if that is before the current time.

However the time comparison requires knowing your region format for time for both SCHTaks nad the local system.

run the attached code and post back the results of the 'running at' portion

and then type this at the command line and post back the resulting line: ECHO %DATE%

ECHO OFF

::Task name is set after /TN
FOR /F "tokens=2,4,6 delims=," %%a IN ('SCHTasks /Query /FO CSV /v /TN "Google Software Updater" ^| FIND /I "Running"') DO ECHO The Task: "%%~na" Has status: "%%~b" and last started at: "%%~c"

Open in new window

0
 
LVL 11

Expert Comment

by:Ben Personick
ID: 33706264
Here you are.  I wrote this script for you, it can be tightened up but this is a working script which will accomplish what you need.

Please note that the script uses "BLAT.exe" which is a free third party tool you can use to send email from the command line. =)

::----------------------------------------------------------------------------::
:: Script: CheckTaskRunTime.bat												  ::
:: Version: 1.0																  ::
:: Copyright: Ben Personick													  ::
:: Date: 2010-09-17															  ::
:: 																			  ::
:: Desc: If a task int he list is Running too long email someone.			  ::
:: 																			  ::
::----------------------------------------------------------------------------::
:Begin-Script
	@ECHO OFF
	::- Set Variables Here.
	SET TaskList="Google Software Updater","GoogleUpdateTaskMachineCore"
	SET "StatusToMatch=Ready"
	SET "MaxMinuts=30"
	SET "DEBUG=OFF"
	SET "BlatCmd=C:\Admin\Blat.exe"
	SET "MailServer=your smtp server's FQDN or IP address"
	SET "MailTo=YourName@yourDomain.com"
	SET "Sender=YourName@yourDomain.com"
	SET "Subject=Nothing"
	SET "Body=Nothing"

	::- Loops through the list of Tasks calling the Main Routine "Start-Task-Check" For Each.
	FOR %%L IN (%TaskList%) DO CALL :Start-Task-Check "%%~L"

	::- Skip the Subroutines and heads to the lable near the end of the script.
	GOTO End-Script

	::- Subroutines Go Here.
	GOTO :EOF
	:Start-Subroutines
		GOTO End-Task-Check
		:Start-Task-Check
			::For Debugging Set Debug=ON
			IF /I "%DEBUG%"=="ON" ECHO.&ECHO.&ECHO ---&ECHO.&ECHO "%~1"
			
			::- Here we loop through the output of SCHTasks For the task, and send it for comparison.
			FOR /F "tokens=2,4,6 delims=, skip=1" %%a IN ('SCHTasks /Query /FO CSV /v /TN "%~1" ^| FIND /I "%StatusToMatch%" ') DO CALL :Start-Compare-Time "%~1" "%%~c"&IF /I "%DEBUG%"=="ON" ECHO.&ECHO Back at Task-Check-Loop, The Task: "%%~na" Has status: "%%~b" and last started at: "%%~c"
			IF /I "%DEBUG%"=="ON" ECHO. &ECHO Finished Checking Task "%~1"
			GOTO :EOF
		:End-Task-Check
		GOTO End-Compare-Time
		:Start-Compare-Time
			SET "RawTime=%~2"
			IF /I "%DEBUG%"=="ON" ECHO "%~2"
			
			::- Cuts up the Task Time into manageble peices.
			FOR /F "Tokens=1-7 Delims=/: " %%A IN ("%RawTime%") DO SET "TskYear=%%C"&SET "TskMMDD=%%A%%B"&SET "TskHH=%%D"&SET "TskTime=0%%E%%F%%G"&SET "TskMM=1%%E"&SET "TskSS=1%%F"&IF /I "%%G"=="PM" SET "TskTime=%%E%%F%%G"&SET /A "TskHH=12+(%%D)"
			IF "%TskMMDD:~3,1%"=="" (SET "TskDate=%TskYear%0%TskMMDD%") ELSE (SET "TskDate=%TskYear%%TskMMDD%")
			
			::- Cuts up the Current Time into manageble peices.
			FOR /F "Tokens=1-7 Delims=/:. " %%A IN ("%DATE% %TIME: =0%") DO SET "NowDate=%%D%%B%%C"&SET "NowTime=%%E%%F%%G"&SET "NowMM=1%%F"&SET "NowSS=1%%G"&SET /A "NowHH=1%%E-100"
			
			::- Debug Lines to check Variables
			IF /I "%DEBUG%"=="ON" ECHO.&ECHO TskDate="%TskDate%" TskHH="%TskHH%" TskMM="%TskMM%" TskSS="%TskSS%"
			IF /I "%DEBUG%"=="ON" ECHO NowDate="%NowDate%" NowHH="%NowHH%" NowMM="%NowMM%" NowSS="%NowSS%"&ECHO.
			
			::- Finds the Fifference from the Time started to the current Time.
			FOR %%l IN ("HH","MM","SS") DO CALL SET /A "Dif%%~l=(60+(%%Now%%~l%%)-(%%Tsk%%~l%%))%%%%60"
			IF /I "%DEBUG%"=="ON" ECHO ECHO DifHH="%DifHH%"&CALL ECHO SET /A "DifHH=(%DifHH%)%%%%24"
			CALL SET /A "DifHH=(%DifHH%)%%%%24"
			
			::- Debug Lines to check Variables
			IF /I "%DEBUG%"=="ON" ECHO.&ECHO.&ECHO TskHH="%TskHH%" TskMM="%TskMM%" TskSS="%TskSS%"
			IF /I "%DEBUG%"=="ON" ECHO NowHH="%NowHH%"	NowMM="%NowMM%"	NowSS="%NowSS%"
			IF /I "%DEBUG%"=="ON" ECHO -----------	-----------	-----------
			IF /I "%DEBUG%"=="ON" ECHO DifHH="%DifHH%"	DifMM="%DifMM%"	DifSS="%DifSS%"&ECHO.
			
			::- Checks if the Time the task was running is too large, and Calls the Function to Send the email:
			IF /I %DifMM% GEQ %MaxMinuts% ( CALL :Start-Mail "%~1" ) ELSE ( IF /I %DifHH% Gtr 0 ( CALL :Start-Mail "%~1" ) ELSE ( IF /I "%TskDate%" NEQ "%NowDate%" ( CALL :Start-Mail "%~1" ) ELSE ( IF /I "%DEBUG%"=="ON" ECHO The Task has Run Less than 30 Minutes.) ) )
			GOTO :EOF
		:End-Compare-Time
		GOTO End-Mail
		:Start-Mail
			IF /I "%DEBUG%"=="ON" ECHO Begining Mailing via Blat!
			SET "Subject=ATTENTION: Task %~1 Running At Least %MaxMinuts% Minutes!"
			SET "Body=The Task %~1 Has Been Running for at least %DifHH% Hours %DifMM% Minutes and %DifSS% Seconds.  The Task was Started on %TskDate% at %TskTime%.  This Alert was Generated on %NowDate% at %NowTime%"&ECHO.
			IF /I "%DEBUG%"=="ON" ECHO "%BlatCmd%" - -body "%Body%" -subject "%Subject%" -to %MailTo% -Mailfrom %Sender% -Server %MailServer%
			"%BlatCmd%" - -body "%Body%" -subject "%Subject%" -to %MailTo% -Mailfrom %Sender% -Server %MailServer%
		GOTO :EOF
		:End-Mail
	:End-Subroutines
:End-Script
IF /I "%DEBUG%"=="ON" ECHO The Script Has Completed!
IF /I "%DEBUG%"=="ON" PAUSE
GOTO :EOF

Open in new window

0
 
LVL 1

Author Comment

by:jimmylew52
ID: 33711544
This look really good. I will give it a try. Thank You!
0
 
LVL 11

Expert Comment

by:Ben Personick
ID: 33711591
Okay, please note, I wrote that script using my date format, your date format may be different, and therefore rquire making chAnges to the script, if you have trouble, turn on debug mode, and also type "Echo %date%" at the command prompt and past the responce here.

Also note, for testing I used the status of "Ready" you need to Change that variable to "Running" when you fill in the correct info on your other variables as well)
0
 
LVL 1

Author Comment

by:jimmylew52
ID: 33712490
I think the date format will be ok. We often use that format. I changed the "Ready" variable to "running" and made changes to the SET commands for email purposes. I will give this a try.
0
 
LVL 1

Author Comment

by:jimmylew52
ID: 33712614
Tried to run the script and got the following errors:

ERROR: Invalid argument/option - '/TN'.
Type "SCHTASKS /QUERY /?" for usage.
'"%~c"' is not recognized as an internal or external command,
operable program or batch file.
ERROR: Invalid argument/option - '/TN'.
Type "SCHTASKS /QUERY /?" for usage.
'"%~c"' is not recognized as an internal or external command,
operable program or batch file.
0
 
LVL 11

Expert Comment

by:Ben Personick
ID: 33712653
Okay, looks like the issue is in the main loop.  I think it's not pulling the task name from you list, the list must be formatted the way I had it, that is

list="Task name", "Task Name", "Task name"

Otherwise it's in the loop where it queries.  I'll get on the computer and check which is more likely.  Also set Debug=ON and see whatere it gets to with that error.
0
 
LVL 1

Author Comment

by:jimmylew52
ID: 33712702
Changed the task names to match my task names and set debugging to on. Recieved the following:

C:\Blat>checktasks.bat


---

"fileRouter"
ERROR: Invalid argument/option - '/TN'.
Type "SCHTASKS /QUERY /?" for usage.

Finished Checking Task "fileRouter"


---

"MPCS (Ercot)"
ERROR: Invalid argument/option - '/TN'.
Type "SCHTASKS /QUERY /?" for usage.

Finished Checking Task "MPCS (Ercot)"
The Script Has Completed!
Press any key to continue . . .
0
 
LVL 1

Author Comment

by:jimmylew52
ID: 33712925
Removed /TN and %~1 from the loop and received this:

C:\Blat>checktasks.bat


---

"MPCS (Ercot)"
"Interactive/Background"

TskDate="InteractiveBackground" TskHH="" TskMM="1" TskSS="1"
NowDate="20100919" NowHH="16" NowMM="121" NowSS="126"

Missing operand.
ECHO DifHH=""
SET /A "DifHH=()%24"
Missing operand.


TskHH="" TskMM="1" TskSS="1"
NowHH="16"      NowMM="121"     NowSS="126"
-----------     -----------     -----------
DifHH=""        DifMM="0"       DifSS="5"

0 was unexpected at this time.
0
 
LVL 11

Expert Comment

by:Ben Personick
ID: 33713006
are you certain these are the task names?  Also it's possible that TN isn't a valid option in Windows 2003..  I'll have to check on that.  Please check on this in the mean-time

Please run this at the command prompt:

SCHTASKS /QUERY

 and see if your tasks are listed.
0
 
LVL 1

Author Comment

by:jimmylew52
ID: 33713041
/TN is not listed when I run SCHTASKS /QUERY /?

results of SCHTASKS /QUERY

C:\Blat>SCHTASKS /QUERY

TaskName                             Next Run Time            Status
==================================== ======================== ===============
CheckRunnungTasks                    2:32:00 PM, 9/20/2010
DELzip                               5:00:00 PM, 9/19/2010
fileRouter                           5:00:00 PM, 9/19/2010
MPCS (Ercot) Process                 Disabled
MPCS (Ercot)                         5:00:00 PM, 9/19/2010    Running
RayburnFTP                           2:55:00 AM, 9/20/2010
TBSSListener                         5:25:00 PM, 9/19/2010
User_Feed_Synchronization-{514C1E8B- 5:01:00 PM, 9/19/2010
User_Feed_Synchronization-{514C1E8B- 5:01:00 PM, 9/19/2010
User_Feed_Synchronization-{D85569E6- 8:01:00 PM, 9/19/2010
User_Feed_Synchronization-{D85569E6- 8:01:00 PM, 9/19/2010
XLATE (Brazos)                       5:00:00 PM, 9/19/2010
XLATE (MeterSmart)                   5:10:00 PM, 9/19/2010
XLATE (Rayburn)                      5:15:00 PM, 9/19/2010
XLATE (Rio Grande)                   5:05:00 PM, 9/19/2010
Zipped_BU                            8:00:00 PM, 9/19/2010
0
 
LVL 1

Author Comment

by:jimmylew52
ID: 33713052
C:\Blat>SCHTASKS /QUERY /?

SCHTASKS /Query [/S system [/U username [/P [password]]]]
         [/FO format] [/NH] [/V] [/?]

Description:
    Enables an administrator to display the scheduled tasks on the
    local or remote system.

Parameter List:
    /S        system         Specifies the remote system to connect to.

    /U        username       Specifies the user context under
                             which the command should execute.

    /P        [password]     Specifies the password for the given
                             user context. Prompts for input if omitted.

    /FO       format         Specifies the format for the output.
                             Valid values: TABLE, LIST, CSV.

    /NH                      Specifies that the column header should not
                             be displayed in the output. This is
                             valid only for TABLE and CSV formats.

    /V                       Displays verbose task output.

    /?                       Displays this help message.

Examples:
    SCHTASKS /Query
    SCHTASKS /Query /?
    SCHTASKS /Query /S system /U user /P password
    SCHTASKS /Query /FO LIST /V /S system /U user /P password
    SCHTASKS /Query /FO TABLE /NH /V
0
 
LVL 11

Expert Comment

by:Ben Personick
ID: 33713064
so, surprise surprise /TN isn't usable in Win2k3 only if you ran this remotely on a 2003 server from a 2008 or win7 woudl that work out.

so to run this on 2003 we'll have to go through a couple more hoops code wise but obviously this will still be doable.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 11

Expert Comment

by:Ben Personick
ID: 33713145
here I believe this will solve your issues.

::----------------------------------------------------------------------------::
:: Script: CheckTaskRunTime.bat												  ::
:: Version: 1.0																  ::
:: Copyright: Ben Personick													  ::
:: Date: 2010-09-17															  ::
::																			  ::
:: Desc: If a task int he list is Running too long email someone.			  ::
::																			  ::
::----------------------------------------------------------------------------::
:Begin-Script
	@ECHO OFF
	::- Set Variables Here.
	SET TaskList="Google Software Updater","GoogleUpdateTaskMachineCore","SR"
	SET "StatusToMatch=Ready"
	SET "MaxMinuts=30"
	SET "DEBUG=ON"
	SET "BlatCmd=C:\Admin\Blat.exe"
	SET "MailServer=your smtp server's FQDN or IP address"
	SET "MailTo=YourName@yourDomain.com"
	SET "Sender=YourName@yourDomain.com"
	SET "Subject=Nothing"
	SET "Body=Nothing"

	::- Loops through the list of Tasks calling the Main Routine "Start-Task-Check" For Each.
	FOR %%L IN (%TaskList%) DO CALL :Start-Task-Check "%%~L"

	::- Skip the Subroutines and heads to the lable near the end of the script.
	GOTO End-Script

	::- Subroutines Go Here.
	GOTO :EOF
	:Start-Subroutines
		GOTO End-Task-Check
		:Start-Task-Check
			::For Debugging Set Debug=ON
			IF /I "%DEBUG%"=="ON" ECHO.&ECHO.&ECHO ---&ECHO.&ECHO "%~1"
			
			::- Here we loop through the output of SCHTasks For the task, and send it for comparison.
			FOR /F "tokens=2,4,6 delims=," %%a IN ('SCHTasks /Query /FO CSV /v ^| FIND /I "%StatusToMatch%" ^| FIND /I "%~1" ') DO CALL :Start-Compare-Time "%~1" "%%~c"&IF /I "%DEBUG%"=="ON" ECHO.&ECHO Back at Task-Check-Loop, The Task: "%%~na" Has status: "%%~b" and last started at: "%%~c"
			IF /I "%DEBUG%"=="ON" ECHO. &ECHO Finished Checking Task "%~1"
			GOTO :EOF
		:End-Task-Check
		GOTO End-Compare-Time
		:Start-Compare-Time
			SET "RawTime=%~2"
			IF /I "%DEBUG%"=="ON" ECHO "%~2"
			
			::- Cuts up the Task Time into manageble peices.
			FOR /F "Tokens=1-7 Delims=/: " %%A IN ("%RawTime%") DO SET "TskYear=%%C"&SET "TskMMDD=%%A%%B"&SET "TskHH=%%D"&SET "TskTime=0%%E%%F%%G"&SET "TskMM=1%%E"&SET "TskSS=1%%F"&IF /I "%%G"=="PM" SET "TskTime=%%E%%F%%G"&SET /A "TskHH=12+(%%D)"
			IF "%TskMMDD:~3,1%"=="" (SET "TskDate=%TskYear%0%TskMMDD%") ELSE (SET "TskDate=%TskYear%%TskMMDD%")
			
			::- Cuts up the Current Time into manageble peices.
			FOR /F "Tokens=1-7 Delims=/:. " %%A IN ("%DATE% %TIME: =0%") DO SET "NowDate=%%D%%B%%C"&SET "NowTime=%%E%%F%%G"&SET "NowMM=1%%F"&SET "NowSS=1%%G"&SET /A "NowHH=1%%E-100"
			
			::- Debug Lines to check Variables
			IF /I "%DEBUG%"=="ON" ECHO.&ECHO TskDate="%TskDate%" TskHH="%TskHH%" TskMM="%TskMM%" TskSS="%TskSS%"
			IF /I "%DEBUG%"=="ON" ECHO NowDate="%NowDate%" NowHH="%NowHH%" NowMM="%NowMM%" NowSS="%NowSS%"&ECHO.
			
			::- Finds the Fifference from the Time started to the current Time.
			FOR %%l IN ("HH","MM","SS") DO CALL SET /A "Dif%%~l=(60+(%%Now%%~l%%)-(%%Tsk%%~l%%))%%%%60"
			IF /I "%DEBUG%"=="ON" ECHO ECHO DifHH="%DifHH%"&CALL ECHO SET /A "DifHH=(%DifHH%)%%%%24"
			CALL SET /A "DifHH=(%DifHH%)%%%%24"
			
			::- Debug Lines to check Variables
			IF /I "%DEBUG%"=="ON" ECHO.&ECHO.&ECHO  TskHH="%TskHH%"      TskMM="%TskMM%"      TskSS="%TskSS%"
			IF /I "%DEBUG%"=="ON" ECHO  NowHH="%NowHH%"      NowMM="%NowMM%"     NowSS="%NowSS%"
			IF /I "%DEBUG%"=="ON" ECHO ------------    ------------    ------------
			IF /I "%DEBUG%"=="ON" ECHO  DifHH="%DifHH%"      DifMM="%DifMM%"      DifSS="%DifSS%"&ECHO.
			
			::- Checks if the Time the task was running is too large, and Calls the Function to Send the email:
			IF /I %DifMM% GEQ %MaxMinuts% ( CALL :Start-Mail "%~1" ) ELSE ( IF /I %DifHH% Gtr 0 ( CALL :Start-Mail "%~1" ) ELSE ( IF /I "%TskDate%" NEQ "%NowDate%" ( CALL :Start-Mail "%~1" ) ELSE ( IF /I "%DEBUG%"=="ON" ECHO The Task has Run Less than 30 Minutes.) ) )
			GOTO :EOF
		:End-Compare-Time
		GOTO End-Mail
		:Start-Mail
			IF /I "%DEBUG%"=="ON" ECHO Begining Mailing via Blat!
			SET "Subject=ATTENTION: Task %~1 Running At Least %MaxMinuts% Minutes!"
			SET "Body=The Task %~1 Has Been Running for at least %DifHH% Hours %DifMM% Minutes and %DifSS% Seconds.  The Task was Started on %TskDate% at %TskTime%.  This Alert was Generated on %NowDate% at %NowTime%"&ECHO.
			IF /I "%DEBUG%"=="ON" ECHO "%BlatCmd%" - -body "%Body%" -subject "%Subject%" -to %MailTo% -Mailfrom %Sender% -Server %MailServer%
			"%BlatCmd%" - -body "%Body%" -subject "%Subject%" -to %MailTo% -Mailfrom %Sender% -Server %MailServer%
			GOTO :EOF
		:End-Mail
	:End-Subroutines
:End-Script
IF /I "%DEBUG%"=="ON" ECHO The Script Has Completed!
IF /I "%DEBUG%"=="ON" PAUSE
GOTO :EOF

Open in new window

0
 
LVL 1

Author Comment

by:jimmylew52
ID: 33713276
Tried the new script and received the following:

C:\Blat>checktasks.bat


---

"MPCS (Ercot)"

Finished Checking Task "MPCS (Ercot)"


---

"filerouter)"

Finished Checking Task "filerouter)"


---

"SR"

Finished Checking Task "SR"
The Script Has Completed!
Press any key to continue . . .

completed except for exiting without errors but no email sent on my task that is hung.
0
 
LVL 1

Author Comment

by:jimmylew52
ID: 33713289
forgot to change SET "StatusToMatch=Running"

after changing that I get the following errors:

C:\Blat>checktasks.bat


---

"MPCS (Ercot)"
"Interactive/Background"

TskDate="InteractiveBackground" TskHH="" TskMM="1" TskSS="1"
NowDate="20100919" NowHH="18" NowMM="105" NowSS="145"

Missing operand.
ECHO DifHH=""
SET /A "DifHH=()%24"
Missing operand.


 TskHH=""      TskMM="1"      TskSS="1"
 NowHH="18"      NowMM="105"     NowSS="145"
------------    ------------    ------------
 DifHH=""      DifMM="44"      DifSS="24"

0 was unexpected at this time.
0
 
LVL 11

Expert Comment

by:Ben Personick
ID: 33713335
hmm looks like there is another issue in regards to Windows 7 verses Windows 2003, I do apologize that since I'm not at work I don't have a 2k3 server easily available to test on.

can you run the attached code and see what it pops out?

:Begin-Script
	@ECHO OFF
	::- Set Variables Here.
	SET TaskList="Google Software Updater","GoogleUpdateTaskMachineCore","SR"
	SET "DEBUG=ON"

	::- Loops through the list of Tasks calling the Main Routine "Start-Task-Check" For Each.
	FOR %%L IN (%TaskList%) DO CALL :Start-Task-Check "%%~L"

	::- Skip the Subroutines and heads to the lable near the end of the script.
	GOTO End-Script

	::- Subroutines Go Here.
	GOTO :EOF
	:Start-Subroutines
		GOTO End-Task-Check
		:Start-Task-Check
			::For Debugging Set Debug=ON
			IF /I "%DEBUG%"=="ON" ECHO.&ECHO.&ECHO ---&ECHO.&ECHO "%~1"
			
			::- Here we loop through the output of SCHTasks For the task, and send it for comparison.
			FOR /F "tokens=* delims=," %%a IN ('SCHTasks /Query /FO CSV /v ^| FIND /I "%StatusToMatch%" ^| FIND /I "%~1" ') DO ECHO OUTPUT IS "%%a"

Open in new window

0
 
LVL 1

Author Comment

by:jimmylew52
ID: 33713376
C:\Blat>test.bat


---

"MPCS (Ercot)"
OUTPUT IS ""CYBER-ECTRENDS","MPCS (Ercot)","7:00:00 PM, 9/19/2010","Running","In
teractive/Background","8:00:00 AM, 9/19/2010","0","bossec","Every 30 minute(s) f
rom 3:30 AM for 18 hour(s) every day, starting 4/21/2010","C:\T2\MPCS\TBComMPCST
ransferTask.bat ","C:\T2\MPCS","N/A","Enabled","Minute ","3:30:00 AM","4/21/2010
","N/A","Everyday","N/A","SYSTRENDS\bossec","Disabled","72:0","30 Minute(s)","No
ne","18 Hour(s): 0 Minute(s)","Disabled","Disabled","No Start On Batteries, Stop
 On Battery Mode""


---

"filerouter"


---

"SR"
The system cannot find the batch label specified - End-Script
0
 
LVL 11

Expert Comment

by:Ben Personick
ID: 33713425
from what you dumped in the last message from the last go round it looks like its simply a matter of the terms used being in an order that's different than the one I'm expecting.

 in fact it looks liek there may be one additional term in the output because the task run time is the 6th term on Windows 7 and the script is spitting out what would be the 5th term on windows 7.
0
 
LVL 11

Expert Comment

by:Ben Personick
ID: 33713454
haha, classic windows =)  the SCHTasks is outputting additional commas in the date/time area than expected ^^

this is honestly a bit trial and error, on this iteration, as again I don't have win2k3 available right his moment to check and I have to step out in a few minutes to have dinner.

  But if there are still issues when I get back I can help, it's honestly just a matter of slight changes to make the date and time work as before.

  Sorry for the trouble mate.

::----------------------------------------------------------------------------::
:: Script: CheckTaskRunTime.bat												  ::
:: Version: 1.2																  ::
:: Copyright: Ben Personick													  ::
:: Date: 2010-09-19															  ::
::																			  ::
:: Desc: If a task int he list is Running too long email someone.			  ::
::																			  ::
::----------------------------------------------------------------------------::
:Begin-Script
	@ECHO OFF
	::- Set Variables Here.
	SET TaskList="Google Software Updater","GoogleUpdateTaskMachineCore","SR"
	SET "StatusToMatch=Ready"
	SET "MaxMinuts=30"
	SET "DEBUG=ON"
	SET "BlatCmd=C:\Admin\Blat.exe"
	SET "MailServer=your smtp server's FQDN or IP address"
	SET "MailTo=YourName@yourDomain.com"
	SET "Sender=YourName@yourDomain.com"
	SET "Subject=Nothing"
	SET "Body=Nothing"

	::- Loops through the list of Tasks calling the Main Routine "Start-Task-Check" For Each.
	FOR %%L IN (%TaskList%) DO CALL :Start-Task-Check "%%~L"

	::- Skip the Subroutines and heads to the lable near the end of the script.
	GOTO End-Script

	::- Subroutines Go Here.
	GOTO :EOF
	:Start-Subroutines
		GOTO End-Task-Check
		:Start-Task-Check
			::For Debugging Set Debug=ON
			IF /I "%DEBUG%"=="ON" ECHO.&ECHO.&ECHO ---&ECHO.&ECHO "%~1"
			
			::- Here we loop through the output of SCHTasks For the task, and send it for comparison.
			FOR /F "tokens=2,4,7,8 delims=," %%a IN ('SCHTasks /Query /FO CSV /v ^| FIND /I "%StatusToMatch%" ^| FIND /I "%~1" ') DO CALL :Start-Compare-Time "%~1" "%%~d %%~c"&IF /I "%DEBUG%"=="ON" ECHO.&ECHO Back at Task-Check-Loop, The Task: "%%~na" Has status: "%%~b" and last started at: "%%~c"
			IF /I "%DEBUG%"=="ON" ECHO. &ECHO Finished Checking Task "%~1"
			GOTO :EOF
		:End-Task-Check
		GOTO End-Compare-Time
		:Start-Compare-Time
			SET "RawTime=%~2"
			IF /I "%DEBUG%"=="ON" ECHO "%~2"
			
			::- Cuts up the Task Time into manageble peices.
			FOR /F "Tokens=1-7 Delims=/: " %%A IN ("%RawTime%") DO SET "TskYear=%%C"&SET "TskMMDD=%%A%%B"&SET "TskHH=%%D"&SET "TskTime=0%%E%%F%%G"&SET "TskMM=1%%E"&SET "TskSS=1%%F"&IF /I "%%G"=="PM" SET "TskTime=%%E%%F%%G"&SET /A "TskHH=12+(%%D)"
			IF "%TskMMDD:~3,1%"=="" (SET "TskDate=%TskYear%0%TskMMDD%") ELSE (SET "TskDate=%TskYear%%TskMMDD%")
			
			::- Cuts up the Current Time into manageble peices.
			FOR /F "Tokens=1-7 Delims=/:. " %%A IN ("%DATE% %TIME: =0%") DO SET "NowDate=%%D%%B%%C"&SET "NowTime=%%E%%F%%G"&SET "NowMM=1%%F"&SET "NowSS=1%%G"&SET /A "NowHH=1%%E-100"
			
			::- Debug Lines to check Variables
			IF /I "%DEBUG%"=="ON" ECHO.&ECHO TskDate="%TskDate%" TskHH="%TskHH%" TskMM="%TskMM%" TskSS="%TskSS%"
			IF /I "%DEBUG%"=="ON" ECHO NowDate="%NowDate%" NowHH="%NowHH%" NowMM="%NowMM%" NowSS="%NowSS%"&ECHO.
			
			::- Finds the Fifference from the Time started to the current Time.
			FOR %%l IN ("HH","MM","SS") DO CALL SET /A "Dif%%~l=(60+(%%Now%%~l%%)-(%%Tsk%%~l%%))%%%%60"
			IF /I "%DEBUG%"=="ON" ECHO ECHO DifHH="%DifHH%"&CALL ECHO SET /A "DifHH=(%DifHH%)%%%%24"
			CALL SET /A "DifHH=(%DifHH%)%%%%24"
			
			::- Debug Lines to check Variables
			IF /I "%DEBUG%"=="ON" ECHO.&ECHO.&ECHO  TskHH="%TskHH%"      TskMM="%TskMM%"      TskSS="%TskSS%"
			IF /I "%DEBUG%"=="ON" ECHO  NowHH="%NowHH%"      NowMM="%NowMM%"     NowSS="%NowSS%"
			IF /I "%DEBUG%"=="ON" ECHO ------------    ------------    ------------
			IF /I "%DEBUG%"=="ON" ECHO  DifHH="%DifHH%"      DifMM="%DifMM%"      DifSS="%DifSS%"&ECHO.
			
			::- Checks if the Time the task was running is too large, and Calls the Function to Send the email:
			IF /I %DifMM% GEQ %MaxMinuts% ( CALL :Start-Mail "%~1" ) ELSE ( IF /I %DifHH% Gtr 0 ( CALL :Start-Mail "%~1" ) ELSE ( IF /I "%TskDate%" NEQ "%NowDate%" ( CALL :Start-Mail "%~1" ) ELSE ( IF /I "%DEBUG%"=="ON" ECHO The Task has Run Less than 30 Minutes.) ) )
			GOTO :EOF
		:End-Compare-Time
		GOTO End-Mail
		:Start-Mail
			IF /I "%DEBUG%"=="ON" ECHO Begining Mailing via Blat!
			SET "Subject=ATTENTION: Task %~1 Running At Least %MaxMinuts% Minutes!"
			SET "Body=The Task %~1 Has Been Running for at least %DifHH% Hours %DifMM% Minutes and %DifSS% Seconds.  The Task was Started on %TskDate% at %TskTime%.  This Alert was Generated on %NowDate% at %NowTime%"&ECHO.
			IF /I "%DEBUG%"=="ON" ECHO "%BlatCmd%" - -body "%Body%" -subject "%Subject%" -to %MailTo% -Mailfrom %Sender% -Server %MailServer%
			"%BlatCmd%" - -body "%Body%" -subject "%Subject%" -to %MailTo% -Mailfrom %Sender% -Server %MailServer%
			GOTO :EOF
		:End-Mail
	:End-Subroutines
:End-Script
IF /I "%DEBUG%"=="ON" ECHO The Script Has Completed!
IF /I "%DEBUG%"=="ON" PAUSE
GOTO :EOF

Open in new window

0
 
LVL 1

Author Comment

by:jimmylew52
ID: 33713546
error looks the same to me

C:\Blat>checktasks.bat


---

"MPCS (Ercot)"
" 9/19/2010"

TskDate="20100919" TskHH="" TskMM="1" TskSS="1"
NowDate="20100919" NowHH="19" NowMM="134" NowSS="113"

Missing operand.
ECHO DifHH=""
SET /A "DifHH=()%24"
Missing operand.


 TskHH=""      TskMM="1"      TskSS="1"
 NowHH="19"      NowMM="134"     NowSS="113"
------------    ------------    ------------
 DifHH=""      DifMM="13"      DifSS="52"

0 was unexpected at this time.
0
 
LVL 11

Expert Comment

by:Ben Personick
ID: 33714370
I will be up and available to work in this in a small number of hours as it's 3:45 AM here and I need to get to work.  I will be able to test the script on windows 2003 server from there and fix accordingly.  sorry for the wait.

~Q
0
 
LVL 1

Author Comment

by:jimmylew52
ID: 33715106
OK, Thanks. Testing will be more difficult. I have to reset the scheduled task this morning as productin will start soon.
0
 
LVL 11

Accepted Solution

by:
Ben Personick earned 500 total points
ID: 33718883
hey, here is the finalized batch script It has been tested and runs on Windows 2003.  Please give it a whirl.

Note in Windows 2003 the "ready" status comes out as "" so I had to test using "Running" but since that is the status you are looking for that works out just fine. =)

~Q

::----------------------------------------------------------------------------::
:: Script: CheckTaskRunTime.bat												  ::
:: Version: 1.2																  ::
:: Copyright: Ben Personick													  ::
:: Date: 2010-09-20															  ::
::																			  ::
:: Desc: If a task int he list is Running too long email someone.			  ::
::																			  ::
::----------------------------------------------------------------------------::
:Begin-Script
	@ECHO OFF
	::- Set Variables Here.
	SET TaskList="some task","SR"
	SET "StatusToMatch=Running"
	SET "MaxMinuts=30"
	SET "DEBUG=ON"
	SET "BlatCmd=C:\Admin\Blat.exe"
	SET "MailServer=your smtp server's FQDN or IP address"
	SET "MailTo=YourName@yourDomain.com"
	SET "Sender=YourName@yourDomain.com"
	SET "Subject=Nothing"
	SET "Body=Nothing"

	::- Loops through the list of Tasks calling the Main Routine "Start-Task-Check" For Each.
	FOR %%L IN (%TaskList%) DO CALL :Start-Task-Check "%%~L"

	::- Skip the Subroutines and heads to the lable near the end of the script.
	GOTO End-Script

	::- Subroutines Go Here.
	GOTO :EOF
	:Start-Subroutines
		GOTO End-Task-Check
		:Start-Task-Check
			::For Debugging Set Debug=ON
			IF /I "%DEBUG%"=="ON" ECHO.&ECHO.&ECHO ---&ECHO.&ECHO "%~1"
			
			::- Here we loop through the output of SCHTasks For the task, and send it for comparison.
			FOR /F "tokens=2,5,7,8 delims=," %%a IN ('SCHTasks /Query /FO CSV /v ^| FIND /I /V "Still Running" ^| FIND /I "%StatusToMatch%" ^| FIND /I "%~1" ') DO ECHO Start-Compare-Time "%~1" "%%d" "%%c"&CALL :Start-Compare-Time "%~1" "%%d" "%%c"&IF /I "%DEBUG%"=="ON" ECHO.&ECHO Back at Task-Check-Loop, The Task: "%%~na" Has status: "%%~b" and last started at: "%%~c"
			IF /I "%DEBUG%"=="ON" ECHO. &ECHO Finished Checking Task "%~1"
			GOTO :EOF
		:End-Task-Check
		GOTO End-Compare-Time
		:Start-Compare-Time
			SET RawTime=%~2%~3
			SET "RawTime=%RawTime:"=%"
			IF /I "%DEBUG%"=="ON" ECHO 1="%~1" 2="%~2" 3="%~3" RawTime="%RawTime%"
			
			::- Cuts up the Task Time into manageble peices.
			FOR /F "Tokens=1-7 Delims=/: " %%A IN ("%RawTime%") DO SET "TskYear=%%C"&SET "TskMMDD=%%A%%B"&SET "TskHH=%%D"&SET "TskTime=0%%E%%F%%G"&SET "TskMM=1%%E"&SET "TskSS=1%%F"&IF /I "%%G"=="PM" SET "TskTime=%%E%%F%%G"&SET /A "TskHH=12+(%%D)"
			IF "%TskMMDD:~3,1%"=="" (SET "TskDate=%TskYear%0%TskMMDD%") ELSE (SET "TskDate=%TskYear%%TskMMDD%")
			
			::- Cuts up the Current Time into manageble peices.
			FOR /F "Tokens=1-7 Delims=/:. " %%A IN ("%DATE% %TIME: =0%") DO SET "NowDate=%%D%%B%%C"&SET "NowTime=%%E%%F%%G"&SET "NowMM=1%%F"&SET "NowSS=1%%G"&SET /A "NowHH=1%%E-100"
			
			::- Debug Lines to check Variables
			IF /I "%DEBUG%"=="ON" ECHO.&ECHO TskDate="%TskDate%" TskHH="%TskHH%" TskMM="%TskMM%" TskSS="%TskSS%"
			IF /I "%DEBUG%"=="ON" ECHO NowDate="%NowDate%" NowHH="%NowHH%" NowMM="%NowMM%" NowSS="%NowSS%"&ECHO.
			
			::- Finds the Fifference from the Time started to the current Time.
			FOR %%l IN ("HH","MM","SS") DO CALL SET /A "Dif%%~l=(60+(%%Now%%~l%%)-(%%Tsk%%~l%%))%%%%60"
			IF /I "%DEBUG%"=="ON" ECHO ECHO DifHH="%DifHH%"&CALL ECHO SET /A "DifHH=(%DifHH%)%%%%24"
			CALL SET /A "DifHH=(%DifHH%)%%%%24"
			
			::- Debug Lines to check Variables
			IF /I "%DEBUG%"=="ON" ECHO.&ECHO.&ECHO  TskHH="%TskHH%"      TskMM="%TskMM%"      TskSS="%TskSS%"
			IF /I "%DEBUG%"=="ON" ECHO  NowHH="%NowHH%"      NowMM="%NowMM%"     NowSS="%NowSS%"
			IF /I "%DEBUG%"=="ON" ECHO ------------    ------------    ------------
			IF /I "%DEBUG%"=="ON" ECHO  DifHH="%DifHH%"      DifMM="%DifMM%"      DifSS="%DifSS%"&ECHO.
			
			::- Checks if the Time the task was running is too large, and Calls the Function to Send the email:
			IF /I %DifMM% GEQ %MaxMinuts% ( CALL :Start-Mail "%~1" ) ELSE ( IF /I %DifHH% Gtr 0 ( CALL :Start-Mail "%~1" ) ELSE ( IF /I "%TskDate%" NEQ "%NowDate%" ( CALL :Start-Mail "%~1" ) ELSE ( IF /I "%DEBUG%"=="ON" ECHO The Task has Run Less than 30 Minutes.) ) )
			GOTO :EOF
		:End-Compare-Time
		GOTO End-Mail
		:Start-Mail
			IF /I "%DEBUG%"=="ON" ECHO Begining Mailing via Blat!
			SET "Subject=ATTENTION: Task %~1 Running At Least %MaxMinuts% Minutes!"
			SET "Body=The Task %~1 Has Been Running for at least %DifHH% Hours %DifMM% Minutes and %DifSS% Seconds.  The Task was Started on %TskDate% at %TskTime%.  This Alert was Generated on %NowDate% at %NowTime%"&ECHO.
			IF /I "%DEBUG%"=="ON" ECHO "%BlatCmd%" - -body "%Body%" -subject "%Subject%" -to %MailTo% -Mailfrom %Sender% -Server %MailServer%
			"%BlatCmd%" - -body "%Body%" -subject "%Subject%" -to %MailTo% -Mailfrom %Sender% -Server %MailServer%
			GOTO :EOF
		:End-Mail
	:End-Subroutines
:End-Script
IF /I "%DEBUG%"=="ON" ECHO The Script Has Completed!
IF /I "%DEBUG%"=="ON" PAUSE
GOTO :EOF

Open in new window

0
 
LVL 1

Author Comment

by:jimmylew52
ID: 33719441
Awesome, works great. I cannot thank you enough.
0
 
LVL 1

Author Closing Comment

by:jimmylew52
ID: 33719448
Not enough words or points you for the help.
0
 
LVL 11

Expert Comment

by:Ben Personick
ID: 33719818
Thankyou!  I really appreciate the kind words!  I'm glad I could help take care of this for you!
 
0
 
LVL 12

Expert Comment

by:FDiskWizard
ID: 33720665
Yeah, QCubed might have earned some Karma points as well on that one ;)
0
 
LVL 26

Expert Comment

by:pony10us
ID: 33721451
:)  Always interesting to watch the development process.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Many of us need to configure DHCP server(s) in their environment. We can do that simply via DHCP console on server or using MMC snap-in on each computer with Administrative Tools installed in a network. But what if we have to configure many DHCP ser…
ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now