在EC2上不执行的用户数据(云初始化)脚本。

时间:2022-09-23 17:30:53

my user-data script

我的用户和数据的脚本

#!
set -e -x
echo `whoami`
su root
yum update -y
touch ~/PLEASE_WORK.txt

which is fed in from the command:

由命令输入:

ec2-run-instances ami-05355a6c -n 1 -g mongo-group -k mykey -f myscript.sh -t t1.micro -z us-east-1a

but when I check the file /var/log/cloud-init.log, the tail -n 5 is:

但是当我检查文件/var/log/cloud-init时。log,尾部- n5是:

[CLOUDINIT] 2013-07-22 16:02:29,566 - cloud-init-cfg[INFO]: cloud-init-cfg ['runcmd']
[CLOUDINIT] 2013-07-22 16:02:29,583 - __init__.py[DEBUG]: restored from cache type DataSourceEc2
[CLOUDINIT] 2013-07-22 16:02:29,686 - cloud-init-cfg[DEBUG]: handling runcmd with freq=None and args=[]
[CLOUDINIT] 2013-07-22 16:02:33,691 - cloud-init-run-module[INFO]: cloud-init-run-module ['once-per-instance', 'user-scripts', 'execute', 'run-parts', '/var/lib/cloud/data/scripts']
[CLOUDINIT] 2013-07-22 16:02:33,699 - __init__.py[DEBUG]: restored from cache type DataSourceEc2

I've also verified that curl http://169.254.169.254/latest/user-data returns my file as intended.

我还验证了curl http://169.254.169.254/latest/user-data会按照预期返回我的文件。

and no other errors or the output of my script happens. how do I get the user-data scrip to execute on boot up correctly?

并且不会发生其他错误或脚本的输出。如何让用户数据脚本在引导时正确执行?

3 个解决方案

#1


14  

Cloud-init does not accept plain bash scripts, just like that. It's a beast that eats YAML file that defines your instance (packages, ssh keys and other stuff).

Cloud-init不接受普通的bash脚本,就像这样。它是一个吃定义实例(包、ssh密钥和其他东西)的YAML文件的怪兽。

Using MIME you can also send arbitrary shell scripts, but you have to MIME-encode them.

使用MIME您还可以发送任意的shell脚本,但是您必须对它们进行MIME编码。

$ cat my-boothook.txt
#!/bin/sh
echo "Hello World!"
echo "This will run as soon as possible in the boot sequence"

$ cat my-user-script.txt
#!/usr/bin/perl
print "This is a user script (rc.local)\n"

$ cat my-include.txt
# these urls will be read pulled in if they were part of user-data
# comments are allowed.  The format is one url per line
http://www.ubuntu.com/robots.txt
http://www.w3schools.com/html/lastpage.htm

$ cat my-upstart-job.txt
description "a test upstart job"
start on stopped rc RUNLEVEL=[2345]
console output
task
script
echo "====BEGIN======="
echo "HELLO From an Upstart Job"
echo "=====END========"
end script

$ cat my-cloudconfig.txt
#cloud-config
ssh_import_id: [smoser]
apt_sources:
 - source: "ppa:smoser/ppa"

$ ls
my-boothook.txt     my-include.txt      my-user-script.txt
my-cloudconfig.txt  my-upstart-job.txt

$ write-mime-multipart --output=combined-userdata.txt \
   my-boothook.txt:text/cloud-boothook \
   my-include.txt:text/x-include-url \
   my-upstart-job.txt:text/upstart-job \
   my-user-script.txt:text/x-shellscript \
   my-cloudconfig.txt

$ ls -l combined-userdata.txt 
-rw-r--r-- 1 smoser smoser 1782 2010-07-01 16:08 combined-userdata.txt

The combined-userdata.txt is the file you want to paste there.

combined-userdata。txt是你想要粘贴的文件。

More info here:

更多信息:

https://help.ubuntu.com/community/CloudInit

https://help.ubuntu.com/community/CloudInit

Also note, this highly depends on the image you are using. But you say it is really cloud-init based image, so this applies. There are other cloud initiators which are not named cloud-init - then it could be different.

还要注意,这在很大程度上取决于您使用的映像。但是你说它是基于云init的图像,所以这是适用的。还有其他的云启动器没有命名为“云初始化”——那么它可能会有所不同。

#2


12  

Actually, cloud-init allows a single shell script as an input (though you may want to use a MIME archive for more complex setups).

实际上,cloud-init允许将单个shell脚本作为输入(尽管您可能希望使用MIME存档来进行更复杂的设置)。

The problem with the OP's script is that the first line is incorrect. You should use something like this:

OP脚本的问题是第一行不正确。你应该这样使用:

#!/bin/sh

The reason for this is that, while cloud-init uses #! to recognize a user script, the operating system needs a complete shebang line in order to execute the script.

原因是,虽然云init使用#!要识别用户脚本,操作系统需要完整的shebang行才能执行脚本。

So what's happening in the OP's case is that cloud-init behaves correctly (i.e. it downloads and tries to run the script) but the operating system is unable to actually execute it.

因此,在OP中所发生的情况是,云init的行为是正确的(例如,它下载并尝试运行脚本),但是操作系统无法实际执行它。


See: Shebang (Unix) on Wikipedia

参见:Shebang (Unix)在*上

#3


2  

This is a couple years old now, but for others benefit I had the same issue, and it turned out that cloud-init was running twice, from inside /etc/rc3.d . Deleting these files inside the folder allowed the userdata to run correctly:

这已经是几年前的事了,但对于其他人来说,我也遇到了同样的问题,结果是,云init运行了两次,从/etc/rc3开始。d。删除文件夹内的这些文件,可以让用户数据正确运行:

lrwxrwxrwx  1 root root   22 Jun  5 02:49 S-1cloud-config -> ../init.d/cloud-config
lrwxrwxrwx  1 root root   20 Jun  5 02:49 S-1cloud-init -> ../init.d/cloud-init
lrwxrwxrwx  1 root root   26 Jun  5 02:49 S-1cloud-init-local -> ../init.d/cloud-init-local

#1


14  

Cloud-init does not accept plain bash scripts, just like that. It's a beast that eats YAML file that defines your instance (packages, ssh keys and other stuff).

Cloud-init不接受普通的bash脚本,就像这样。它是一个吃定义实例(包、ssh密钥和其他东西)的YAML文件的怪兽。

Using MIME you can also send arbitrary shell scripts, but you have to MIME-encode them.

使用MIME您还可以发送任意的shell脚本,但是您必须对它们进行MIME编码。

$ cat my-boothook.txt
#!/bin/sh
echo "Hello World!"
echo "This will run as soon as possible in the boot sequence"

$ cat my-user-script.txt
#!/usr/bin/perl
print "This is a user script (rc.local)\n"

$ cat my-include.txt
# these urls will be read pulled in if they were part of user-data
# comments are allowed.  The format is one url per line
http://www.ubuntu.com/robots.txt
http://www.w3schools.com/html/lastpage.htm

$ cat my-upstart-job.txt
description "a test upstart job"
start on stopped rc RUNLEVEL=[2345]
console output
task
script
echo "====BEGIN======="
echo "HELLO From an Upstart Job"
echo "=====END========"
end script

$ cat my-cloudconfig.txt
#cloud-config
ssh_import_id: [smoser]
apt_sources:
 - source: "ppa:smoser/ppa"

$ ls
my-boothook.txt     my-include.txt      my-user-script.txt
my-cloudconfig.txt  my-upstart-job.txt

$ write-mime-multipart --output=combined-userdata.txt \
   my-boothook.txt:text/cloud-boothook \
   my-include.txt:text/x-include-url \
   my-upstart-job.txt:text/upstart-job \
   my-user-script.txt:text/x-shellscript \
   my-cloudconfig.txt

$ ls -l combined-userdata.txt 
-rw-r--r-- 1 smoser smoser 1782 2010-07-01 16:08 combined-userdata.txt

The combined-userdata.txt is the file you want to paste there.

combined-userdata。txt是你想要粘贴的文件。

More info here:

更多信息:

https://help.ubuntu.com/community/CloudInit

https://help.ubuntu.com/community/CloudInit

Also note, this highly depends on the image you are using. But you say it is really cloud-init based image, so this applies. There are other cloud initiators which are not named cloud-init - then it could be different.

还要注意,这在很大程度上取决于您使用的映像。但是你说它是基于云init的图像,所以这是适用的。还有其他的云启动器没有命名为“云初始化”——那么它可能会有所不同。

#2


12  

Actually, cloud-init allows a single shell script as an input (though you may want to use a MIME archive for more complex setups).

实际上,cloud-init允许将单个shell脚本作为输入(尽管您可能希望使用MIME存档来进行更复杂的设置)。

The problem with the OP's script is that the first line is incorrect. You should use something like this:

OP脚本的问题是第一行不正确。你应该这样使用:

#!/bin/sh

The reason for this is that, while cloud-init uses #! to recognize a user script, the operating system needs a complete shebang line in order to execute the script.

原因是,虽然云init使用#!要识别用户脚本,操作系统需要完整的shebang行才能执行脚本。

So what's happening in the OP's case is that cloud-init behaves correctly (i.e. it downloads and tries to run the script) but the operating system is unable to actually execute it.

因此,在OP中所发生的情况是,云init的行为是正确的(例如,它下载并尝试运行脚本),但是操作系统无法实际执行它。


See: Shebang (Unix) on Wikipedia

参见:Shebang (Unix)在*上

#3


2  

This is a couple years old now, but for others benefit I had the same issue, and it turned out that cloud-init was running twice, from inside /etc/rc3.d . Deleting these files inside the folder allowed the userdata to run correctly:

这已经是几年前的事了,但对于其他人来说,我也遇到了同样的问题,结果是,云init运行了两次,从/etc/rc3开始。d。删除文件夹内的这些文件,可以让用户数据正确运行:

lrwxrwxrwx  1 root root   22 Jun  5 02:49 S-1cloud-config -> ../init.d/cloud-config
lrwxrwxrwx  1 root root   20 Jun  5 02:49 S-1cloud-init -> ../init.d/cloud-init
lrwxrwxrwx  1 root root   26 Jun  5 02:49 S-1cloud-init-local -> ../init.d/cloud-init-local