title: Rebooting From CloudFormation::Init Command
body: <p>I'm having trouble rebooting my EC2 instance from a <code>cfn-init</code> command. I have the following config key in my instance's <code>CloudFormation::Init</code> metadata.</p>
  
  <pre><code>dns-hostname:
    commands:
      dns-hostname:
        env: { publicDns: !Ref PublicDns }
        command: |
          old=$(hostname)
          sed "s|HOSTNAME=localhost.localdomain|HOSTNAME=$publicDns|" --in-place /etc/sysconfig/network
          echo HOSTNAME changed from \"$old\" to \"$publicDns\"
          reboot
        ignoreErrors: true
  </code></pre>
  
  <p>All the command is supposed to do is change the instance's hostname to the provided public DNS name. A reboot is required for this change to take effect, and since <code>cfn-init</code> doesn't know this, I have to include the actual call to <code>reboot</code> in the last line. Unfortunately, the build fails with the following log message (from <code>/var/log/cfn-init.log</code>):</p>
  
  <pre><code>2017-04-16 12:16:00,301 [DEBUG] Running command dns-hostname
  2017-04-16 12:16:00,301 [DEBUG] Running test for command dns-hostname
  2017-04-16 12:16:00,309 [DEBUG] Test command output: HOSTNAME will be changed to "bastion.example.com"
  2017-04-16 12:16:00,309 [DEBUG] Test for command dns-hostname passed
  2017-04-16 12:16:00,321 [ERROR] Command dns-hostname (old=$(hostname)
  sed "s|HOSTNAME=localhost.localdomain|HOSTNAME=$publicDns|" --in-place /etc/sysconfig/network
  echo HOSTNAME changed from \"$old\" to \"$publicDns\"
  reboot
  ) failed
  2017-04-16 12:16:00,321 [DEBUG] Command dns-hostname output: HOSTNAME changed from "ip-10-0-128-4" to "bastion.example.com"
  /bin/sh: line 3: reboot: command not found
  2017-04-16 12:16:00,321 [INFO] ignoreErrors set to true, continuing build
  </code></pre>
  
  <p>Clearly, the actual hostname change is not failing, just the call to <code>reboot</code>. I get the same error message if I try to use <code>shutdown -r</code> instead of <code>reboot</code>, and if I try to use an absolute path (<code>sbin/reboot</code>), then it just hangs and stack creation times out.  How are these very basic commands not found?  Am I missing something simple here? Any help is appreciated!</p>
  
  <p><strong>EDIT</strong>:  According to <a href="https://askubuntu.com/questions/186808/every-command-fails-with-command-not-found-after-changing-bash-profile">this post</a>, when common commands are not available, it may be due to a screwed up <code>PATH</code>.  And indeed, the <a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-init.html#aws-resource-init-commands" rel="nofollow noreferrer">CloudFormation::Init docs</a> say that using the <code>env</code> property will overwrite the current environment, potentially including <code>PATH</code>.  However, I added a line to my template to <code>echo $PATH</code> inside the command, and that yielded: "<code>usr/local/bin:/bin:/usr/bin</code>".  So my <code>PATH</code> still includes the path to the <code>bash</code> executable, and I am still confused...</p>