简体   繁体   English

来自 Cloud-Init 执行的日志不存在

[英]Logs from Cloud-Init Execution are not present

So I've got an EC2 instance with a user-data script, that is running from a snapshot turned to a custom AMI.所以我有一个带有用户数据脚本的 EC2 实例,它从快照运行到自定义 AMI。 It uses cloud-boothook , because custom AMI's don't trigger user-data, as they're not being launched for the first time.它使用cloud-boothook ,因为自定义 AMI 不会触发用户数据,因为它们不是第一次启动。

I really need to have a clear output of user-data execution to debug any NPM failures in the future.我真的需要有一个明确的 output 用户数据执行,以便将来调试任何 NPM 故障。

However neither cloud-init.log or cloud-init-output.log contain anything from script execution.然而,cloud-init.log 或 cloud-init-output.log 都不包含脚本执行的任何内容。

There's also no boot.log present, as some other solutions refer to it.也没有 boot.log 存在,因为其他一些解决方案引用了它。

#cloud-boothook
#!/bin/bash
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
...
npm i
npm run build
...

Below you may see /var/log/ contents您可能会在下面看到 /var/log/ 内容

var log content变量日志内容

I ran into the same problem, and I can see in the cloud-init source code that stdout and stderr for boothook scripts are just thrown away because the default is capture=True on subp.subp.我遇到了同样的问题,我可以在 cloud-init 源代码中看到 boothook 脚本的 stdout 和 stderr 被丢弃了,因为默认值是 subp.subp 上的 capture=True。 Perhaps this is intentional since boothooks run on every boot and the logs could grow without bound?也许这是故意的,因为 boothooks 会在每次启动时运行并且日志可以无限增长?

I think your only option is to redirect stdout and stdin, either by invoking yourself with redirections (and an env var to prevent recursion), or replace handles in place with something like:我认为你唯一的选择是重定向 stdout 和 stdin,或者通过重定向调用你自己(和一个 env var 来防止递归),或者用类似的东西替换句柄:

# Close standard output file descriptor
exec 1<&-
# Close standard error file descriptor
exec 2<&-

# Open standard output as $LOG_FILE file for read and write.
exec 1<>$LOG_FILE

# Redirect standard error to standard output
exec 2>&1

echo "This line will appear in $LOG_FILE, not go to original stderr"

source code:源代码:

https://github.com/canonical/cloud-init/blob/bae9b11da9ed7dd0b16fe5adeaf4774b7cc628cf/cloudinit/handlers/boot_hook.py#L51 https://github.com/canonical/cloud-init/blob/bae9b11da9ed7dd0b16fe5adeaf4774b7cc628cf/cloudinit/handlers/boot_hook.py#L51

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM