Using Startup Task with command line redirection in Windows Azure Role

Recently while working on an incident I found that if you use Windows Azure Startup task with launch some application along with command line redirection (to collect log) directly as below, it may not launch the startup task:

ServiceDefinition.csdef:

    1: <Task commandLine="Startup\Application.exe >>  C:\temp\logfilename.log 2>&1" executionContext="elevated" taskType="background" relativePath="approot\bin" />

 

As startup task will not be launched, there will not be any command line redirection log collected. After little more work I found that most of the std* redirection are done directly from BATCH/CMD script in the Startup task and works fine. Redirection directly from startup commandline is not suggested and to solve this problem what you can do is create a launcher.bat and place the command + redirection code to get it working.

 

Step 1: First create a batch file which includes the command line as required (Launcher.bat)

    1:  @ECHO ON
    2:  @ECHO "Launcher"
    3:  md C:\temp
    4:  Startup\Application.exe  >> C:\temp\logfilename.log 2>&1

 

Step 2: After it modify the ServiceDefinition to use launcher.bat as commandline as below:

  
    1: <Task commandLine="Startup/launcher.bat" executionContext="elevated" taskType="background" relativePath="approot\bin" />

 

After I modified the startup code as above and did test in a web role with Windows Azure VM everything worked fine.