Using Windows Azure SDK 1.3 or later you have ability to launch a process (called startup task) depend on your choices such as:
To add a Startup task to your role you just need to add the <Startup> <task /></Startup> entries in the Service Definition file (ServiceDefinition.csdef) similar to as below:
<ServiceDefinition name="MyService" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
<Task commandLine="Startup.cmd" executionContext="limited" taskType="simple">
In above example the Startup.cmd is a properly create CMD file (or it could be a batch file or an EXE itself) which contain the startup application.
The commandLine attribute is the executable File Name, which is going to execute as startup task.
In Above the executionContext attribute can be:
In above setting the taskType attribute can be:
Here are a few scenarios to understand it much better:
<Task commandLine="Startup1.cmd" executionContext="limited" taskType="simple"/>
<Task commandLine="Startup2.cmd" executionContext="limited" taskType="simple"/>
In this situation First Startup1.cmd task will execute and when this task is complete and exit then Startup2.cmd will executed. Finally when this task is complete exit the Role will start.
<Task commandLine="Startup1.cmd" executionContext="limited" taskType="background"/>
In this situation First Startup1.cmd task will execute and just after Startup2.cmd will execute. Startup2.cmd task will not depend on Startup1.cmd task, however the Role will not start until Startup2.cmd task is complete and exit.
Running Startup tasks in User Context:
You must understand that these Startup tasks defined in Service Definition file, does not run in any user context and it is possible that when application you are going to run as Startup task needs an user account to execute otherwise it may return "Access Denied" Error. So you may think how to accomplish it. The only solution is the create a user account first within your startup task and then launch the startup task within the newly created user account. David Aiken has written great article along with code snippet about running Startup task as a real user as below:
Debugging with Startup task along with Tips and Tricks:
Debug your application in compute emulator along with startup tasks and batch or command files is tricky task and no one knows better that Steve Marx so he created a very nice article about trips and trick using Startup Task as below:
How to use Startup task in Azure by Windows Azure Documentation Team: