6

I am new to cloud-init, my final goal is to run an R script each time an EC2 Spot Instance becomes active, but in order to test it I created an on-demand Ubuntu 12.04 instance and created a simple script but nothing happens after reboot. Here are the steps I took:

  • Launched the new Ubunut 12.04 instance
  • Navigate to /var/lib/cloud/scripts/per-boot
  • sudo vi script.sh
  • Added the following code:

#!/bin/sh
echo "test"

  • sudo reboot

At this point I thought I should see a "test" print when the instance reboots, but there is nothing there. I went to take a look at /var/log/cloud-init.log but there is no error or anything out of the ordinary.

I am clearly missing something so any tip in the right direction will be much appreciated!

Thanks!

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
JordanBelf
  • 3,208
  • 9
  • 47
  • 80
  • I finally managed to do this using `rc.local`, this doesn't answer my original question as I still don't know why the script was not loading, but it might help someone in the same situation as mine – JordanBelf Sep 03 '12 at 19:02

4 Answers4

8

Looked at the source code, the script must be executable. This'll make it work

sudo chmod 744 script.sh

tn.33
  • 81
  • 1
  • 2
5

The accepted answer is not adequate imo, so after I spent my whole morning getting to the bottom of this I will write my view since I believe people will find this useful and save time.

If the scripts-user module is set to always run then the runcmd: section of your cloudinit will run at every boot.

This can be done with by adding the following section on your cloud-config file,

cloud_final_modules:
 - [scripts-user, always]

If you want to run certain scripts at every boot then you need to place under the

/var/lib/cloud/scripts/per-boot/ folder. To achieve that, add the following section to your cloud-config file,

write_files:
  - content: |
      #!/bin/bash
      echo "Hello World.  The time is now $(date -R)!"
    path: /var/lib/cloud/scripts/per-boot/myScript.sh
    permissions: "0755"

Now everytime I reboot my EC2 instance it will run myScript.sh

And a full example of cloud-config that installs amazon ssm agent on Rhel 8

#cloud-config
cloud_final_modules:
 - rightscale_userdata
 - scripts-per-once
 - scripts-per-boot
 - scripts-per-instance
 - scripts-user
 - keys-to-console
 - phone-home
 - final-message

write_files:
  - content: |
      #!/bin/bash
      echo "Hello World.  The time is now $(date -R)!"
    path: /var/lib/cloud/scripts/per-boot/myScript.sh
    permissions: "0755"


runcmd:
  - sudo dnf install -y python3
  - sudo yum install -y https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/linux_amd64/amazon-ssm-agent.rpm
  - sudo systemctl enable amazon-ssm-agent
  - sudo systemctl start amazon-ssm-agent

Please note that in this case myScript.sh precedes the execution of runcmd: on first boot and subsequent boots only execute myScript.sh

furydrive
  • 372
  • 2
  • 5
  • Well, all very interesting, but surely a better answer would be rc.d/cron/whatever. I can't immediately see any use whatever for a cloud-init per-boot script. Is there one? – EML Jun 04 '23 at 19:17
3

If we want to debug the user-data in cloud-init, we can try the following steps:

  1. rm -rf /var/lib/cloud/*
  2. cloud-init init
  3. cloud-init modules -m final

With above commands, the cloud-init is re-run. And we can check the cloud-init.log under /var/log/cloud-init.log to check if it is successfully exec.

Amio.io
  • 20,677
  • 15
  • 82
  • 117
  • 2
    After doing this once I was getting an error `IsADirectoryError: [Errno 21] Is a directory: '/var/lib/cloud/instance'` on repeat runs of the module. I didn't want to lose/recreate my `/var/lib/cloud/scripts/per-boot` scripts so I experimented and found that `sudo rm -rf /var/lib/cloud/instance /var/lib/cloud/instances/*; sudo cloud-init modules -m final` is the minimal cleanup needed to allow me to retest. Thanks for the lead! – Bruno Bronosky Feb 19 '18 at 18:17
2

It won't run unless the scripts user is set to always run. See this answer for more details (and for instructions on how to get scripts to run on reboot, generally).

Community
  • 1
  • 1
Christopher
  • 42,720
  • 11
  • 81
  • 99
  • 5
    Is this correct? This question is about `/var/lib/cloud/scripts/per-boot/*` and the question you link to is about getting `/var/lib/cloud/instance/user-data.txt` to run on every boot. The last statement in the answer suggests using the former in place of the later. – Bruno Bronosky Feb 19 '18 at 21:14