Run commands during boot¶
Both runcmd
and bootcmd
can be used to run commands during the boot
process. They contain either a list of lists or a list of strings. Each item is
run in order, with output printed to the console.
The list must be written in proper YAML – be sure to quote any characters
such as colons (:
) that would otherwise be “eaten”.
runcmd
only runs on first boot, after the instance has been started and all other configuration has been applied. In general, you should useruncmd
unless you need to run something earlier in boot.bootcmd
runs on every boot, and is typically used to run commands very early in the boot process (just after a boothook, and often before other cloud-init modules have run).
For a full list of keys for these two modules, refer to the runcmd module and bootcmd module schema.
Run commands on instance initialization¶
1#cloud-config
2runcmd:
3- [ls, -l, /]
4- [sh, -xc, 'echo $(date) '': hello world!''']
5- [sh, -c, echo "=========hello world'========="]
6- ls -l /root
Note
Don’t write files to /tmp
from cloud-init – use /run/somedir
instead. Early boot environments can race systemd-tmpfiles-clean
(LP:
#1707222).
Run commands in early boot¶
The cloud-init-per
command can be used to make bootcmd
run exactly
once.
1#cloud-config
2bootcmd:
3- echo 192.168.1.130 us.archive.ubuntu.com > /etc/hosts
4- [cloud-init-per, once, mymkfs, mkfs, /dev/vdb]