← Back to team overview

cloud-init team mailing list archive

Re: cloud-init run command executed on every boot


Hello Scott and thank you for the reply,
the second option  "put scripts into /var/lib/cloud/scripts/per-boot/"
worked as expected. So I put a script under  /var/lib/cloud/scripts/per-boot/
and it worked.
My question now is where am I going to store under /var/lib/cloud the
"my-write-files" you attached which is in yml form so that I have these two
log files written in the directories specified within the my-write-files.
Does the file need to have a  my-write-files need to have a suffix .yml?
Thanks in advance

On Mon, May 14, 2018 at 11:14 PM, Scott Moser <smoser@xxxxxxxxxx> wrote:

> On Fri, May 11, 2018 at 3:20 AM, Konstantinos Papadopoulos <
> kostis.g.papadopoulos@xxxxxxxxx> wrote:
>> Hello cloud-init experts. I have two basic question regarding cloud-init
>> I have recently created an ubuntu server vm (using VBox) and I want to
>> use the cloud-init service specifically the runcmd functionality. I am
>> creating a file foo.yaml having the content below
>> #cloud-config
>> # run commands
>> # default: none
>> # runcmd contains a list of either lists or a string
>> # each item will be executed in order at rc.local like level with
>> # output to the console
>> # - runcmd only runs during the first boot
>> # - if the item is a list, the items will be properly executed as if
>> #   passed to execve(3) (with the first arg as the command).
>> # - if the item is a string, it will be simply written to the file and
>> #   will be interpreted by 'sh'
>> #
>> # Note, that the list has to be proper yaml, so you have to quote
>> # any characters yaml would eat (':' can be problematic)
>> runcmd:
>>  - [ ls, -l, / ]
>>  - [ sh, -xc, "echo $(date) ': hello world!'" ]
>>  - [ sh, -c, echo "=========hello world'=========" ]
>>  - ls -l /root
>>  - [ wget, "http://slashdot.org";, -O, /tmp/index.html ]
>> The question is. How can I configure my vm via cloud init so that the
>> aforementioned runcmd is executed *everytime* the vm boots.
> Hi Konstantinos,
> a 'runcmd' will run once per instance.  So each newly deployed instance
> will have those commands run once.
> Think if runcmd as "instance initialization".
> If you want something to run every boot, then you have 2 options:
> a.) bootcmd .  that runs earlier in the boot process, and runs every
> boot.  it looks same as 'runcmd' in format.
> b.) put scripts into /var/lib/cloud/scripts/per-boot/
> Here is an example http://paste.ubuntu.com/p/w8QQjN4dPC/ (and attached)
> of using write_files to write files into those directories.
> The per-once script will only run once (you can remove the file
> /root/my-per-once.log and reboot to verify).
> The per-boot script will run every boot.
>> The second question is how will I use this foo.yml so that the this
>> runcmd  is executed only once when the the vm is created. The vm will be
>> created through openstack and Vbox.
> I think runcmd is probably what you want.  If you deploy a new instance
> from this image, you'd probably want to run your scripts again.
> If that is not the case, and what you really do want is once *ever*, then
> you can put things in /var/lib/cloud/scripts/per-once/ .  That directory
> will have 'run-parts' run on it by cloud-init only once ever (based on
> presence of /var/lib/cloud/sem/config_scripts_per_once.once).


Konstantinos G. Papadopoulos, Dr-Eng.
Software Development Engineer
Intracom Telecom S.A., IXG
Marinou Antipa 41, Pilaia,
Thessaloniki, Greece, GR 57001
tel   : +30-2310-497459 (int.: 3959)
e-mail: konstantinos.papadopoulos@xxxxxxxxxxxx
      : konstantinos.papadopoulos@xxxxxxxxxxx
      : konpap@xxxxxxxxxxxxxxxxxxxx
url   : www.intracom-telecom.com