SCHTASKS - Job return code instead of return code of Job creation /execution

J

James D Smooth

Is there a way to get a response from a Batch Program that is created
by using the command SCHTASKS? I have an outside scheduler that is
running batch programs, one of which creates a scheduled task to
execute a batch program that crosses several servers. Is there a way
to get a return code from this type of batch program that will provide
a return code once the program that is executed by the SCHTASKS
command completes. Right now I'm able to get a return code, but it is
just simply telling me if the task was created with or without issue.
 
P

Pegasus \(MVP\)

James D Smooth said:
Is there a way to get a response from a Batch Program that is created
by using the command SCHTASKS? I have an outside scheduler that is
running batch programs, one of which creates a scheduled task to
execute a batch program that crosses several servers. Is there a way
to get a return code from this type of batch program that will provide
a return code once the program that is executed by the SCHTASKS
command completes. Right now I'm able to get a return code, but it is
just simply telling me if the task was created with or without issue.

If you use schtasks.exe to create a task (e.g. sales.job) then that task
will run at some time in the future. It is therefore not possible for
schtasks to report on the success or otherwise of sales.job. It cannot look
into the future!
 
B

billious

James D Smooth said:
Is there a way to get a response from a Batch Program that is created
by using the command SCHTASKS? I have an outside scheduler that is
running batch programs, one of which creates a scheduled task to
execute a batch program that crosses several servers. Is there a way
to get a return code from this type of batch program that will provide
a return code once the program that is executed by the SCHTASKS
command completes. Right now I'm able to get a return code, but it is
just simply telling me if the task was created with or without issue.

Need more information here.

Is it the case that BATCH1 uses SCHTASKS to schedule BATCH2?

Do you then want BATCH1 to wait for BATCH2 to complete (with some sort of
status?)

Perhaps you could use

del filagfile 2>nul
schtasks....
:loop
ping -n 11 127.0.0.1 >nul
if not exist flagfile goto loop
for /f "delims=" %%i in (flagfile) do set b2return=%%i


and BATCH2 should write a text line to flagfile when it terminates - the
result should appear in %b2return%

Or perhaps you could cause BATCH3 to be scheduled for
BATCH2starttime+BATCH2maxexecutiontime, put BATCH1 remainder in BATCH3 and
terminate BATCH1 after scheduling BATCH3

Or perhaps you could simply CALL BATCH2 from BATCH1. BATCH1 will then wait
until BATCH2 is finished - and ERRORLEVEL or an environment variable set in
BATCH2 can then be directly read by BATCH1 when it resumes execution
 
J

James D Smooth

got it to work like you suggested

SET FILENAME="C:\Temp\Test1.bat"
SET ANYERRCODE=0

SCHTASKS /Create /S cledt-123616 /RU usag\rcheek /RP Camaro78 /TR
%FILENAME% /TN test_proc /SC ONCE /ST 00:00:00 /SD 12/31/2020

IF NOT %ERRORLEVEL%==0 SET ANYERRCODE=%ERRORLEVEL%

SCHTASKS /Run /S CLEDT-123616 /TN test_proc

:loop
IF NOT EXIST "C:\Documents and Settings\rcheek\Desktop\test.txt" goto
loop

echo REBOOT OK >>"C:\Documents and Settings\rcheek\Desktop\log.txt"
SCHTASKS /Delete /S cledt-123616 /TN test_proc /F


EXIT /B %ANYERRCODE%
 
B

billious

James D Smooth said:
got it to work like you suggested

SET FILENAME="C:\Temp\Test1.bat"
SET ANYERRCODE=0

SCHTASKS /Create /S cledt-123616 /RU usag\rcheek /RP Camaro78 /TR
%FILENAME% /TN test_proc /SC ONCE /ST 00:00:00 /SD 12/31/2020

IF NOT %ERRORLEVEL%==0 SET ANYERRCODE=%ERRORLEVEL%

SCHTASKS /Run /S CLEDT-123616 /TN test_proc

:loop
IF NOT EXIST "C:\Documents and Settings\rcheek\Desktop\test.txt" goto
loop

echo REBOOT OK >>"C:\Documents and Settings\rcheek\Desktop\log.txt"
SCHTASKS /Delete /S cledt-123616 /TN test_proc /F


EXIT /B %ANYERRCODE%

You really should include the

PING -n 11 127.0.0.1 >nul

line in the loop.

As you have it, there is a hard loop which will eat cycles waiting for the
file to appear.

The PING line above will effectively sleep for about 10 seconds, using
minimal resources. It waits for about ((number following -n) - 1 seconds)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top