What's the right way to run startup task for Azure Virtual Machine What's the right way to run startup task for Azure Virtual Machine powershell powershell

What's the right way to run startup task for Azure Virtual Machine


I've already tried a lot of ways to achieve this goal, and from my point of view, Task Scheduler is the best choice to do this kind of job.

The key point is never let the Windows Command Prompt exist after starting your process. For example, at first we use forever to launch node application. Since the forever command returns quickly, which means all the command execution finished. Thus, the Windows Command Prompt process will be killed, and along with the node process.

What we did is, we use another way to launch forever (forever-monitor), which is used in the code. By doing this, we can simply use node command to start this forever startup file. Since the node command will block the command prompt, the node application won't be killed anymore.


Another alternative is use Azure Automation remotely run the startup script.

The full script can be found here, key point is:

$PSCommandResult = Invoke-command -AsJob -ConnectionUri $Using:Uri.AbsoluteUri -credential $Using:mycreds -ScriptBlock { Invoke-Expression "D:\start.ps1" }

In start.ps1 script, we also should not exit the execution, which we can use Start-Sleep command to achieve this. The script can be:

forever start --sourceDir D: start.jsStart-Sleep -s 86400

Hope this two approaches can help you.