651.288.7000 info@intertech.com
By Jim White (Director of Training and instructor)

Before a piece of code starts on a server, you often need to initialize the environment that will host that piece of code.  For example, you may need to install other software, register a component, start another process, etc.  The same might be true even when your code is a Windows Azure role and it is running in the cloud.  Windows Azure SDK 1.3 added the ability to run startup tasks with elevated privileges to access restricted features of the operating system and have more control over running the instance.

Startup tasks allow you to run an initialization script or program (batch file, PowerShell script, etc.) on the hosting virtual machine before your role is started.  Startup tasks are created and added to your role project.

How to Add Startup Tasks to your Cloud Project

To add a startup task to your cloud project, simply create a task and add it to your Azure role project of your cloud solution in Visual Studio.imageAs a simple example, the GetIPConfig.cmd script below simply gathers the ipconfig information and stores it in a file called c:\ipdata.txt.

ipconfig > c:\\ipdata.txt

Obviously, your script can and probably will be more complex.  As a note of warning, VS attaches a byte order mark to all files by default.  A byte code order is a Unicode character used to signal the endianness (or byte order) of a text file.  You do not want the byte code order in your simple script/text files (which is the basis for most script files).  Therefore, make sure you select ?Advanced Save Options?? from the file menu before saving script files.imageMake sure the script files are saved with Unicode (UTF-8 without signature) ? Codepage 650001 encoding.image

You want to make sure the task gets deployed with your cloud project and copied to the right location on the virtual machine.  Using the Properties editor in VS, configure the file to ?Copy to Output Directory.?imageThis causes the file to be copied to bin folder of your role.  Windows Azure looks in the bin folder for the startup tasks.

Register the Task in the Service Definition

Once you have a startup task file in your role project, register and configure the task in the service definition file.

Notice the <Startup> element (and therefore the <Task> element) is a child element of the role element in the service definition file.  This indicates that each role can have zero to many startup tasks.  However, the startup task is specific to a role.  The commandLine attribute specifies the name of the script or program to execute before the role starts.  The executionContext attribute determines the level of permissions needed for the startup task.  Options include limited and elevated.  Under limited permissions, the task runs with the same privileges as the role.  Whereas, elevated permissions causes the task to run with administrative privileges. 

Task Execution

Lastly, the taskType indicates how the task should run.  Options include simple, foreground, or background execution.  A simple task type executes synchronously.  In other words, the task blocks execution of the role until the task completes.  Simple is the default taskType.  When run as a background type of task, the task launches and then Azure starts the role immediately.  In other words, background tasks are asynchronous.  Foreground task types are also asynchronous.  However, the role cannot be shutdown until all startup such tasks exit/complete.

The startup tasks will execute on your development machine when you run the solution in the Compute Emulator.  It is particularly important to test simple tasks since they will block role startup if they do not complete successfully.  Once running in the cloud, you may want to use Remote Desktop to check that the startup tasks executed correctly.

image

See here for more information on how to setup your roles for Remote Desktop.

Wrap Up

For more information about Windows Azure Startup Tasks see the MSDN site.  If you are just starting out in Windows Azure, consider taking Intertech’s Complete Windows Azure class.  I just completed the third version of this class.  Here is a list of the new features covered.

?    All text and labs have been updated to cover the new Windows Azure Developer Portal

?    All labs have been updated to use Windows Azure SDK ver. 1.3

?    A chapter on Windows Azure Administration was added to include material on subscriptions, how to set up and utilize co-administrators, understanding Windows Azure OS Family and Guest OS, Remote Desktop to Windows Azure virtual machines, and more.

?    A new lab was added to try Remote Desktop into an Azure virtual machine.

?    Lab material was added to explore co-administration of Windows Azure.

?    A new lab was added to explore SQL Azure and tools for creating and exploring databases in the cloud.

?    Lab material was added to explore how to see how to publish to Azure directly through Visual Studio (bypassing  the Developer Portal).

?    Material was added to explore Web roles with multiple Web sites.

?    All labs and text have been updated to include Visual Basic code samples and lab solutions (in addition to C# samples and lab solutions).

?    An explanation of the new extra small VM and use of the extra small VM in labs.

?    A quick look at the VM Role and how it relates to the other parts of Windows Azure Compute.

?    A quick look at Windows Azure Startup Tasks.

?    A look at using IntelliTrace in Visual Studio to debug and examine applications running in the cloud.

If your team needs help implementing an Azure solution, contact Ryan McCabe (Intertech’s account representative for Azure) at rmccabe@intertech.com.  Finally, I also encourage you to register with the Virtual Azure User Group (azureug.net).  We meet monthly in virtual space and share our knowledge and experiences on Azure.

Pin It on Pinterest

Share This

Like What You've Read?

Subscribe to the Blog.

Every Friday we send that week's content from our Developers via email. Try it out!

Some ad blockers can block the form below.

You have Successfully Subscribed!