Cloud-init per-boot script doing nothing
Asked Answered
N

4

8

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!

Neiman answered 2/9, 2012 at 22:17 Comment(1)
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 mineNeiman
A
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).

Antemeridian answered 26/2, 2013 at 22:10 Comment(1)
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.Gen
A
9

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

sudo chmod 744 script.sh

Appoint answered 22/10, 2020 at 4:18 Comment(0)
E
9

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

Elagabalus answered 2/6, 2022 at 12:59 Comment(2)
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?Squeegee
Pretty useful for customising Ubuntu Multipass instances.Oosphere
R
5

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.

Requisite answered 30/10, 2016 at 15:17 Comment(1)
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!Gen
A
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).

Antemeridian answered 26/2, 2013 at 22:10 Comment(1)
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.Gen

© 2022 - 2024 — McMap. All rights reserved.