显然我在这里做错了。
云初始化脚本/etc/cloud/cloud.cfg
...
runcmd:
- [ sh, /opt/cloud-init-scripts/whatever.sh ]
脚本/opt/cloud-init-scripts/whatever.sh
#!/bin/bash
...
. /home/ubuntu/third-party/script.sh --silent
第三方脚本/home/ubuntu/third-party/script.sh
#!/usr/bin/env bash
function some_function() {
...
我进入 /var/log/cloud-init-output.log
时出错
/opt/cloud-init-scripts/whatever.sh: 3: /home/ubuntu/third-party/script.sh: Syntax error: "(" unexpected
我一定是遗漏了一些明显的东西。我尝试在调用第三方脚本时使用 source
、.
和 sh
,尝试在所有地方更改 shebangs 但没有成功。
如果我从命令行运行相同的命令,它会起作用。
最佳答案
您在 runcmd
下指定了 sh
shell,但将 she-bang 设置为 bash
。后者无关紧要,因为如果您以 sh/opt/cloud-init-scripts/whatever.sh
运行,它将使用 sh shell 运行。我猜您可能正在使用与 sh
shell 不兼容的非 POSIX
shell 功能。
或者,如果您打算在 bash
shell 中运行脚本,请将 cloud-init
脚本中的 runCmd
更改为
runcmd:
- [ bash, /opt/cloud-init-scripts/whatever.sh ]
关于linux - 无法使 bash 脚本从 cloud-init 运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46297120/