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 )
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>”>
           <Task commandLine=”FileToExecute.Extension”  executionContext=”elevated” taskType=”simple”>
           <Task commandLine=”FileToExecute2.Extension”  executionContext=”elevated” taskType=”simple”>
           <Task commandLine=”FileToExecute3.Extension”  executionContext=”elevated” taskType=”simple”>
            <Instances count=”<number-of-instances>” />
                       <Setting name=”<setting-name>” value=”<setting-value>” />
                     <Certificate name=”<certificate-name>” thumbprint=”<certificate-thumbrint>” thumbprintAlgorithm=”<algorithm>” />
           <OsImage href=”<vhd_image_name>” />
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 
Cloud Cover Episode 31 – 17 Minutes in
Avkash Chauhan’s Blog
Share on Facebook

Leave a Reply

Your email address will not be published.