问题描述
我在这里关注了几篇文章,试图在每次引导后不仅在第一次引导后在ec2实例上运行python或shell脚本.
I have followed a few posts on here trying to run either a python or shell script on my ec2 instance after every boot not just the first boot.
我尝试过:
[始终是脚本用户]到/etc/cloud/cloud.cfg文件
- 已将脚本添加到./scripts/per-boot文件夹中
和
将脚本添加到/etc/rc.local
- 是的,/etc/rc.local的权限已更改为755
我正在尝试将文件的输出传递到/home/ubuntu/目录中的文件中,并且引导后该文件不包含任何内容.
I am attempting to pipe the output of the file into a file located in the /home/ubuntu/ directory and the file does not contain anything after boot.
如果我手动运行脚本(.sh或.py),它们将起作用.
If I run the scripts (.sh or .py) manually they work.
是否有任何建议或要求其他信息的帮助?
Any suggestions or request for additional info to help?
推荐答案
因此,当前的解决方案似乎是我在最初的问题帖子中注销的一种方法,因为我可能未完全按照以下链接中的概述进行设置. ..
So the current solution appears to be a method I wrote off in my initial question post as I may have not performed the setup exactly as outline in the link below...
此链接->如何操作我让EC2实例每次启动时都运行cloud-init启动脚本吗?
链接显示了如何修改/etc/cloud/cloud.cfg 文件以将脚本用户更新为 [脚本用户,始终]
The link shows how to modify the /etc/cloud/cloud.cfg file to update scripts-user to [scripts-user, always]
该链接还显示将* .sh文件添加到/var/lib/cloud/scripts/per-boot 目录.
Also that link says to add your *.sh file to /var/lib/cloud/scripts/per-boot directory.
重新启动系统后,脚本应已执行,您可以在以下地址进行验证: sudo cat/var/log/cloud-init.log
Once you reboot your system your script should have executed and you can verify this in: sudo cat /var/log/cloud-init.log
如果脚本仍然无法执行,请尝试使用以下命令擦除服务器的实例状态: sudo rm -rf/var/lib/cloud/instance/*
if your script still fails to execute try to erase the instance state of your server with the following command: sudo rm -rf /var/lib/cloud/instance/*
-注意:-似乎来自python脚本的打印命令未按预期进行管道(>>),但回显命令却容易管道
--NOTE:--It appears print commands from a python script do not pipe (>>) as expected but echo commands pipe easily
管道失败sudo python test.py >> log.txt
管道成功echo "HI" >> log.txt
这篇关于ec2在每次启动时运行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!