简体   繁体   English

如何在AWS EC2服务器上启动时运行命令(python文件)

[英]How run a command (python file) on boot on AWS EC2 server

I'm having some problem making a python file run everytime the AWS server boots. 我在每次AWS服务器引导时都无法运行python文件时遇到问题。

I am trying to run a python file to start a web server on Amazon Webservice EC2 server. 我正在尝试运行python文件以在Amazon Webservice EC2服务器上启动Web服务器。

But I am limited to edit systemd folder and other folders such as init.d 但是我仅限于编辑systemd文件夹和其他文件夹,例如init.d

Is there anything wrong? 有什么问题吗?

Sorry I don't really understand EC2's OS, it seems a lot of methods are not working on it. 抱歉,我不太了解EC2的操作系统,似乎很多方法都无法解决。

What I usually do via ssh to start my server is: 我通常通过ssh来启动服务器的操作是:

python hello.py

Can anyone tell me how to run this file automatically every time system reboots? 有人可以告诉我如何在每次系统重新启动时自动运行此文件吗?

It depends on oyour linux OS but you are on the right track in init.d. 这取决于您的Linux操作系统,但是init.d处在正确的位置。 This is exactly where you'd want to run arbitrary shell scripts on start up. 这正是您要在启动时运行任意shell脚本的地方。

Here is a great HOWTO and explanation: https://www.tldp.org/HOWTO/HighQuality-Apps-HOWTO/boot.html 这是一个很棒的HOWTO和说明: https : //www.tldp.org/HOWTO/HighQuality-Apps-HOWTO/boot.html

and another stack overflow specific to running a python script: 和另一个特定于运行python脚本的堆栈溢出:

Run Python script at startup in Ubuntu 在Ubuntu中启动时运行Python脚本

if you want to share you linux OS i can be more specific. 如果您想与您共享Linux OS,我可以更具体一些。

EDIT: This may help, looks like they have some sort of launch wizard: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html 编辑:这可能会有所帮助,看起来它们具有某种启动向导: https : //docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html

When you launch an instance in Amazon EC2, you have the option of passing user data to the instance that can be used to perform common automated configuration tasks and even run scripts after the instance starts. 在Amazon EC2中启动实例时,可以选择将用户数据传递到该实例,该数据可用于执行常见的自动配置任务,甚至在实例启动后运行脚本。 You can pass two types of user data to Amazon EC2: shell scripts and cloud-init directives. 您可以将两种类型的用户数据传递给Amazon EC2:shell脚本和cloud-init指令。 You can also pass this data into the launch wizard as plain text, as a file (this is useful for launching instances using the command line tools), or as base64-encoded text (for API calls). 您还可以将这些数据以纯文本,文件(对于使用命令行工具启动实例的方式)或以base64编码的文本(用于API调用)传递给启动向导。

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

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