如何在aws EC2实例(ubuntu)上运行python脚本?

时间:2022-12-04 07:53:03

I have an ec2 instance"abc1"(ubuntu) and I create image from abc1. Then,I launch a new instance "abc2" from abc1 image. I want abc2 to run the script when it will be launched.
So,I write the text in userdata:

我有一个ec2实例“abc1”(ubuntu),我从abc1创建图像。然后,我从abc1映像启动一个新的实例“abc2”。我希望abc2在脚本启动时运行它。我用userdata写文本:

#!/bin/bash
cd ~/env/xxx/bin/
source activate
python3.6 ~/env/xxx/source/test.py

I manually use these commands,it's valid.However,it doesn't work when using userdata automatically. Does anyone know how to adjust my settings? Thanks a lot.

我手动使用这些命令,它是有效的。但是,当自动使用userdata时,它不能工作。有人知道怎么调整我的设置吗?非常感谢。

1 个解决方案

#1


2  

My guess is that you are ssh'ing into the ec2 instance as a different user than the instance is running it at, so the HOME directory (~) is not the same when run manually vs run with cloudinit.

我的猜测是,您正在ssh到ec2实例,因为与实例在其上运行的用户不同,所以当手动运行与使用cloudinit运行时,主目录(~)是不同的。

Try writing the script using absolute paths, not relative paths.

尝试使用绝对路径编写脚本,而不是相对路径。

#1


2  

My guess is that you are ssh'ing into the ec2 instance as a different user than the instance is running it at, so the HOME directory (~) is not the same when run manually vs run with cloudinit.

我的猜测是,您正在ssh到ec2实例,因为与实例在其上运行的用户不同,所以当手动运行与使用cloudinit运行时,主目录(~)是不同的。

Try writing the script using absolute paths, not relative paths.

尝试使用绝对路径编写脚本,而不是相对路径。