Define Startup Tasks for a Role
Updated: May 9, 2014
You can define a startup task for a role by adding the Startup element to the definition of the role in the service definition file. For more information about these elements, see the Azure Service Definition Schema (.csdef File).
You can edit the ServiceDefinition.csdef file by using your favorite text editor, or you can define the tasks for the role by using Visual Studio.
Open the ServiceDefinition.csdef file for your service in the text editor.
Add the Startup element to the WebRole or WorkerRole; startup tasks are not applicable to the VMRole. The following code example shows how to add a startup task to a web role that runs a command script:
<ServiceDefinition name="MyService" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition"> <WebRole name="WebRole1"> <Startup> <Task commandLine="Startup.cmd" executionContext="limited" taskType="simple"> </Task> </Startup> </WebRole> </ServiceDefinition>
Note The commandLine task is relative to the AppRoot\Bin folder for the deployment. Environment variables are not expanded in the commandLine value. If environment expansion is required, you can create a small .cmd script that calls your startup task as cmd.exe, which expands the environment variables.
Set the commandLine value to the name of the program or script file that you want to run.
Set the executionContext to the level of permissions that you want for your startup task.
Set the taskType to simple, foreground, or background depending on how you want the task to run. For more information on taskType, see WorkerRole Schema.
Save the file.
ConceptsRun Startup Tasks in Azure