How do you wait on a Task Scheduler task to finish in a batch file or C

0 votes

I am trying to write a batch file that does two things:

  1. First it launches an installer (install.exe) which installs a program (program.exe).
  2. Second it launches an instance of the installed program (program.exe). This must be executed after the installation completes.

This would be relatively straightforward except that the installer needs administrator privileges and must run in a user context. Even with these restrictions this would still be relatively straightforward except that I am running this on an Azure Worker Role, which means two things:

  1. Elevated batch files must be run from a startup task.
  2. There is no user context for startup tasks in Azure worker roles.

It therefore appears that the solution is to run the installer as a Task Scheduler task in a real user context. However, this comes with the caveat that I would then need to wait for this task to finish before I could launch program.exe.

Thus, my question is: How do I wait on a Task Scheduler task to finish in a batch file?

Alternatively, I could daisy chain the two calls in my batch file within a single Task Scheduler task (Task Scheduler supports up to 16 sequential events in a single task [Citation Needed]).

However, if I take this approach my batch file will terminate as soon as the task is scheduled, not as soon as it finishes. Unfortunately, I must wait on it to finish before I can do any of the logic in my Worker Role. However, if I can check if a Task Scheduler task has finished from C# (WITHOUT admin privileges), then I can just wait on it there.

Thus a second viable answer would be to the question: How do I check if a Task Scheduler task has completed from C#?

Sep 24, 2018 in Azure by null_void
• 3,220 points
2,844 views

1 answer to this question.

0 votes

From batch file, query the task status, and if it is running, keep querying

:loop
for /f "tokens=2 delims=: " %%f in ('schtasks /query /tn yourTaskName /fo list ^| find "Status:"' ) do (
    if "%%f"=="Running" (
        ping -n 6 localhost >nul 2>nul
        goto loop
    )
)
answered Sep 24, 2018 by club_seesharp
• 3,450 points

Related Questions In Azure

0 votes
1 answer

How to Schedule a task in Azure Worker role?

You can simply have a message on ...READ MORE

answered Jul 1, 2019 in Azure by Perry
• 17,100 points
1,432 views
0 votes
1 answer

How to create a deployment from a remote template file in Azure?

Hi@akhtar, You can create a deployment from a ...READ MORE

answered Nov 15, 2020 in Azure by MD
• 95,460 points
557 views
+1 vote
2 answers

How do I establish an connection to Azure IoT Hub? Say a connection like MQTT/AMQP using Python.

Azure IoT Hub comes with 3 SDK's: Service SDK Device ...READ MORE

answered Apr 13, 2018 in Azure by null_void
• 3,220 points
2,215 views
0 votes
2 answers

How to process a file using Azure Function?

You don't really need a http trigger ...READ MORE

answered Jun 18, 2018 in Azure by club_seesharp
• 3,450 points
7,293 views
0 votes
1 answer

How to identify a deadlock in SQL Azure?

Monitoring of SQL Azure is more limited ...READ MORE

answered Jun 19, 2018 in Azure by club_seesharp
• 3,450 points
2,636 views
0 votes
1 answer

How to get the list of names of Azure blob files in a container?

We can get some additional info like Size, ...READ MORE

answered Jun 27, 2018 in Azure by club_seesharp
• 3,450 points
21,816 views
0 votes
2 answers

How to deploy a simple static micro site on Microsoft Azure?

Create an Azure Storage account ("hot file" ...READ MORE

answered Jun 4, 2020 in Azure by René
1,378 views
0 votes
1 answer

How to upload a file on to Azure Blob storage without writing a code?

You can find the below tools useful ...READ MORE

answered Apr 13, 2018 in Azure by club_seesharp
• 3,450 points
1,599 views
0 votes
1 answer

Azure Blob: How to open a file in browser without downloading it?

First, because I was using a byte[] the controller ...READ MORE

answered Jun 20, 2018 in Azure by club_seesharp
• 3,450 points
24,664 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP