AWS EC2用户数据运行Python脚本不起作用

发布于 2025-01-27 10:27:44 字数 1245 浏览 1 评论 0原文

我正在尝试在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.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

┈┾☆殇 2025-02-03 10:27:44

您可以在脚本开始时使用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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文