Run Startup Tasks in Azure

Startup Tasks on an Azure instance before the Role Fires up.
(Also there is a nice little codeplex project at  http://Startuptaskrunner.codeplex.com )
 
You can create a user then use the RunsAs command within a script if needed.
 
Make sure your commands are able to re-run without problem since you may come back up in a VM that you were previously in therefore you could try to install something that may throw an error if bootstrapped again.
 
in the CSDef (Service Definition) file within the Role Element you can add a <Startup/> command and have it fire off Executables, Batches, Commands(CMD, not command line) etc.
 
<ServiceConfiguration serviceName=”<service-name>” osFamily=”2” osVersion=”<os-version>”>
   <Role name=”<role-name>”>
     <Startup>
           <Task commandLine=”FileToExecute.Extension”  executionContext=”elevated” taskType=”simple”>
           <Task commandLine=”FileToExecute2.Extension”  executionContext=”elevated” taskType=”simple”>
           <Task commandLine=”FileToExecute3.Extension”  executionContext=”elevated” taskType=”simple”>
     </Startup>
            <Instances count=”<number-of-instances>” />
            <ConfigurationSettings>
                       <Setting name=”<setting-name>” value=”<setting-value>” />
            </ConfigurationSettings>  
            <Certificates>
                     <Certificate name=”<certificate-name>” thumbprint=”<certificate-thumbrint>” thumbprintAlgorithm=”<algorithm>” />
            </Certificates>
           <OsImage href=”<vhd_image_name>” />
      </Role>
</ServiceConfiguration>
 
 
The commandLine is a link to the actual file you would like to execute.
The executionContext can be “elevated” or  “limited” 
        *elevated uses NT AuthoritySystem  or Administrator Role
        * Limited uses same level of privileges as the rest of the Application.
 The taskType can be left off since the default is simple, however if you would like the task to run in order (The startup before the role starts), you will need to keep it marked as simple or remove the taskType all together.  You can also set Background or Foreground tasks.  A background task will run in parallel with the Role instance.  The Foreground tasktype will keep the role running as long as the foreground task is running.  
 
 
From Avkash’s Blog

Important Notes: 

  • For task type simple, they are expected to return an exit code 0 to indicate successful completion. Without 0, the next task or role will not start.
  • If the commands in the cmd file do not adhere to convention of returning 0 for success, you may need to add “exit /b 0” to force it. Reference http://blog.smarx.com/posts/asp-net-mvc-in-windows-azure 
References:
Cloud Cover Episode 31 – 17 Minutes in
Avkash Chauhan’s Blog
 
Share on Facebook

Leave a Reply

Your email address will not be published. Required fields are marked *