简体   繁体   English

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

[英]AWS EC2 User Data to run python script is not working

I am trying to run a python script on EC2 Linux machine.我正在尝试在 EC2 Linux 机器上运行 python 脚本。 All I want is on every start of the instance it should run this python script/file called my_file.py.我想要的只是在实例的每次启动时它应该运行这个名为 my_file.py 的 python 脚本/文件。 it's in my ec2-user directory.它在我的 ec2-user 目录中。 I also tried running manually and it works fine by running the command 'python3 my_file.py'.我也尝试过手动运行,运行命令“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/我一直在关注如何从这篇文章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.我试图查看 /var/log/cloud-init-output.log 文件,但由于权限被拒绝问题而无法打印它。 Attaching a picture here.在这里附上一张图片。 Not sure what's the issue or am I missing something here.不确定是什么问题,或者我在这里遗漏了什么。 Thanks.谢谢。

在此处输入图像描述

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您可以在脚本的开头使用sudo -i ,否则您必须以 root 用户身份使用脚本来执行命令

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM