1

I have created two task Task1 and Task2 through a .bat file using the command schtasks.exe.

I need to create a trigger that will trigger the start of Task2 when Task1 is completed.

Where and how do I write the commands for the trigger in the batch file?

If I create the trigger manually in the Windows Task Scheduler then following is the XML I use in the Trigger tab of Task2.

<QueryList>
  <Query Id="0" Path="Microsoft-Windows-TaskScheduler/Operational">
    <Select Path="Microsoft-Windows-TaskScheduler/Operational">*[EventData[@Name='TaskSuccessEvent'][Data[@Name='TaskName']='\Task1']]</Select>
  </Query>
</QueryList>

Here is the content of the batch file:

@echo off
Set RUN_AS_ACCT=%USERDOMAIN%\%USERNAME%

echo The currently logged on user is: %RUN_AS_ACCT%
echo.
set /P INP_RUN_AS_ACCT="Account to run the batch under?(%RUN_AS_ACCT%) "

IF NOT "%INP_RUN_AS_ACCT%"=="" SET RUN_AS_ACCT=%INP_RUN_AS_ACCT%
echo.
echo Using: %RUN_AS_ACCT%
schtasks.exe /CREATE /RU "%RUN_AS_ACCT%" /RP /TN "Task1" /tr "D:\load\Task1.bat"
echo.
schtasks.exe /CREATE /RU "%RUN_AS_ACCT%" /RP /TN "Task2" /tr "D:\load\Task2.bat"
echo.
pause
:EOF
Ora Aff
  • 640
  • 1
  • 9
  • 24

1 Answers1

2

You could create a schedule to run the following:

cmd /c start /wait task1.exe && start task2.exe

When task1.exe closes, task2 will automatically start.

Bali C
  • 30,582
  • 35
  • 123
  • 152
  • to be [exact](https://ss64.com/nt/syntax-redirection.html): "when task1 exits with errorlevel 0, task 2 will automatically start" – Stephan Apr 04 '17 at 14:47
  • 1
    @Stephan Interesting, I didn't know that! – Bali C Apr 04 '17 at 14:54
  • I have added the batch file content in the OP. @Bali, Task1 will be executed manually. The batch file in question should not start Task1. Sorry for not being specific earlier. – Ora Aff Apr 04 '17 at 15:12
  • 1
    @OraAff That's fine, you just need to run a scheduled task for the line of code I put above, instead of the individual tasks, unless I'm not following you? – Bali C Apr 04 '17 at 15:26
  • I am able to do what I need with "cmd /c start /wait task1.exe && start task2.exe" as a scheduled task but the command window of Task1 when it finishes waits for the user to exit before it starts the Task2. – Ora Aff Apr 05 '17 at 13:09
  • @OraAff I don't think there's much you can do then as you need some way of finding out when task1 is finished, is there something it does you can look for as an indication? – Bali C Apr 05 '17 at 13:15
  • I wonder why the command window still remains there for the user to terminate it because we have provided the cmd /c in our script ! From the help: "/C Carries out the command specified by string and then terminates" Does not "terminates" here mean closing of the command window? – Ora Aff Apr 05 '17 at 13:34
  • Yeah it should, but is the app asking for some input before closing? – Bali C Apr 05 '17 at 13:40
  • Basically Task1 and Task 2 are batch files. Task1 loads an EDW and Task2 loads a Data Mart. If I run the batch files manually without the task scheduler, the command window terminates itself without user's input. – Ora Aff Apr 05 '17 at 13:48
  • 1
    @OraAff That's weird, can you try this, slightly modified `cmd /c start /wait cmd /c task1.exe && start task2.exe`. – Bali C Apr 05 '17 at 14:07
  • If I do that then Task2 starts while Task1 is still running because Task1.bat has start "" command in it that starts the actual loading of the EDW. – Ora Aff Apr 05 '17 at 14:38
  • Can you just change task1 to wait on the `start ""` command, then run task2 after? – Bali C Apr 05 '17 at 14:45
  • I will have to explore the possibility of modifying the Task1.bat. Not sure if that will be allowed but thanks for all your suggestions. They are very helpful. – Ora Aff Apr 05 '17 at 14:50