AWS EC2用户数据运行Python脚本不起作用
我正在尝试在EC2 Linux机器上运行Python脚本。我想要的只是在实例的每个开始中,都应该运行此python脚本/文件,称为my_file.py。它在我的EC2用户目录中。我还尝试了手动运行,并且通过运行命令“ python3 my_file.py”来正常运行。
我一直在关注如何编写本文中的用户数据 https ://aws.amazon.com/premuimsupport/knowledge-center/execute-user-data-ec2/
我的用户数据代码:
Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0
--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"
#cloud-config
cloud_final_modules:
- [scripts-user, always]
--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"
#!/bin/bash
python3 /home/ec2-user/my_file.py
--//--
我看不到此文件在此实例的启动上运行。我试图查看/var/log/cloud-init-actup.log文件,但由于拒绝的问题而无法打印它。在这里附加图片。不确定是什么问题,或者我在这里错过了什么。谢谢。
I am trying to run a python script on EC2 Linux machine. All I want is on every start of the instance it should run this python script/file called my_file.py. it's in my ec2-user directory. I also tried running manually and it works fine by running the command 'python3 my_file.py'.
I have been following how to write user data from this article https://aws.amazon.com/premiumsupport/knowledge-center/execute-user-data-ec2/
My user data code :
Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0
--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"
#cloud-config
cloud_final_modules:
- [scripts-user, always]
--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"
#!/bin/bash
python3 /home/ec2-user/my_file.py
--//--
I don't see this file running on the startup of this instance. I tried to see /var/log/cloud-init-output.log file but can't print it due to a Permission denied issue. Attaching a picture here. Not sure what's the issue or am I missing something here. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在脚本开始时使用
sudo -i
,否则您必须将脚本用作root用户才能执行命令you can use
sudo -i
at the start of the script or else you have to use the script as a root user to execute a command