我需要一些跨终点的持久性存储。
我解决问题的方法是
编写一个启动脚本来附加和挂载持久性卷
快照此状态并注册一个私人AMI
启动基于专用AMI的竞价型实例
在这个论坛[ 1 ]的search表明这是正确的程序
我的启动脚本:
#!/bin/sh echo "executing startup script" # attach the EBS volume to this machine aws ec2 attach-volume --volume-id vol-7bef1d96 --instance-id $(wget -q -O - http://169.254.169.254/latest/meta-data/instance-id ) --device /dev/xvdg sleep 10 # mount the attached EBS volume echo "mounting the attached volume" sudo mount /dev/xvdg1 /home/ubuntu/persistent/ #run script echo "starting the dummy program in background" python2 persistent/dummyProgram.py &
如果手动执行,该脚本完美工作。 但是,当它由cron执行时,只有echo语句被执行
crontab的任务是
@reboot /path/to/startupScript.sh
我如何解决这个问题?
更正的crontab条目如下:
USER=username PATH=content of $PATH @reboot /path/to/startupScript.sh
从crontab脚本调用命令时不运行的最可能的原因是它们不在PATH环境variables上。 Crontab条目默认情况下使用最less的一组环境variables运行。
虽然cron
完全可以完成工作,但是执行此操作的规范方法是指定在启动实例实例时运行的user-data
脚本。 使用AWS CLI的request-spot-instance
,使用--launch-specification
参数在JSON中指定。
http://docs.aws.amazon.com/cli/latest/reference/ec2/request-spot-instances.html
这部分可以是一个小的shell脚本,允许你改变volume-id ,而不是硬编码到AMI中。
为什么不把它放入userdata而不是cron? 这就是为什么他们是为了:在启动时执行一些代码。 通过使用cron,你暴露自己的path问题,你可能不会有用户数据