{"id":122,"date":"2024-01-05T10:11:01","date_gmt":"2024-01-05T18:11:01","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/?p=122"},"modified":"2024-01-05T10:38:43","modified_gmt":"2024-01-05T18:38:43","slug":"when-will-customscript-extension-re-execute-my-script","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/when-will-customscript-extension-re-execute-my-script\/","title":{"rendered":"When will CustomScript extension re-execute my script?"},"content":{"rendered":"<p>One of the lesser known differences between <a href=\"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/runcommand-vs-custom-script-extension-vs-vm-applications\/\">RunCommand and CustomScriptExtension<\/a> is the fact that we\u00a0<em>do<\/em> promise to not re-run your script in RunCommand, but no such promise exists for CustomScript. This is mentioned in the <a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/virtual-machines\/extensions\/custom-script-windows\">documentation<\/a>, which isn&#8217;t often fully understood.<\/p>\n<ul>\n<li>Write scripts that are idempotent, so that running them more than once accidentally doesn&#8217;t cause system changes.<\/li>\n<\/ul>\n<p>However, more than once I&#8217;ve been asked: when does CSE\u00a0<em>actually<\/em> re-run the script?<\/p>\n<p>The answer is, it\u00a0<em>may<\/em> run on a reboot. This can happen if your script never finished running. This is actually by design, since many scripts run by CSE may reboot the machine. So, in that case the scripts runs, installs some stuff, reboots the machine, then we call it again so it may complete the setup. Note that we say <em>may<\/em> because the OS may not accept the request to reboot immediately, so it depends on how your script is written. If you\u00a0<em>really<\/em> want to continue after a reboot, use DesiredStateConfiguration (DSC) instead of CSE.<\/p>\n<p>RunCommand is very different here. Once your script starts, regardless what happens, we won&#8217;t rerun it &#8211; even if it reboots the machine.<\/p>\n<p>So, for a comparison:<\/p>\n<table style=\"border-collapse: collapse; width: 87.2136%;\">\n<tbody>\n<tr>\n<td style=\"width: 22.7273%;\"><strong>Extension<\/strong><\/td>\n<td style=\"width: 64.4863%;\"><strong>Reboot Behavior<\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 22.7273%;\">CustomScript<\/td>\n<td style=\"width: 64.4863%;\">May re-run the script if it didn&#8217;t finish.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 22.7273%;\">RunCommand<\/td>\n<td style=\"width: 64.4863%;\">Will not re-run the script even if it didn&#8217;t finish.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 22.7273%;\">VMApplications<\/td>\n<td style=\"width: 64.4863%;\">Will re-run the application operation if it didn&#8217;t finish.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>So, that&#8217;s when CustomScript will re-execute. That\u00a0<em>seems<\/em> simple, but the problem is you often can&#8217;t guarantee that the machine won&#8217;t be rebooted when running it. Numerous times I&#8217;ve seen a script start executing, then\u00a0<em>something else<\/em> reboot the machine. This could be patching, some other process on the VM, or an operator. When the script comes back up, it executes\u00a0<em>from the beginning<\/em> and causes havoc &#8211; or just fails.<\/p>\n<p>For that reason, we advise all CSE scripts to be idempotent. You may think you have total control over whether the VM will reboot while executing your script, but you don&#8217;t.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the lesser known differences between RunCommand and CustomScriptExtension is the fact that we\u00a0do promise to not re-run your script in RunCommand, but no such promise exists for CustomScript. This is mentioned in the documentation, which isn&#8217;t often fully understood. Write scripts that are idempotent, so that running them more than once accidentally doesn&#8217;t [&hellip;]<\/p>\n","protected":false},"author":103696,"featured_media":72,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[7,6],"tags":[8,9],"class_list":["post-122","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-customscriptextension","category-runcommand","tag-customscriptextension","tag-runcommand"],"acf":[],"blog_post_summary":"<p>One of the lesser known differences between RunCommand and CustomScriptExtension is the fact that we\u00a0do promise to not re-run your script in RunCommand, but no such promise exists for CustomScript. This is mentioned in the documentation, which isn&#8217;t often fully understood. Write scripts that are idempotent, so that running them more than once accidentally doesn&#8217;t [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/wp-json\/wp\/v2\/posts\/122","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/wp-json\/wp\/v2\/users\/103696"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/wp-json\/wp\/v2\/comments?post=122"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/wp-json\/wp\/v2\/posts\/122\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/wp-json\/wp\/v2\/media\/72"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/wp-json\/wp\/v2\/media?parent=122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/wp-json\/wp\/v2\/categories?post=122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-vm-runtime\/wp-json\/wp\/v2\/tags?post=122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}