简体   繁体   English

Ubuntu EC2 - 重新启动时运行 Python 脚本

[英]Ubuntu EC2 - Run Python script on reboot

I have an Ubuntu 16.04 EC2 Instance and I need to run a python script every time the instance is Started.我有一个 Ubuntu 16.04 EC2 实例,每次启动实例时我都需要运行 python 脚本。 I tried everything suggested on every question in the forum and haven't had any luck yet.我尝试了论坛中每个问题的所有建议,但还没有运气。

Specifically I've tested:具体来说,我已经测试过:

  • Adding @reboot python3 /home/project/script.py to crontab@reboot python3 /home/project/script.py添加到 crontab
  • Adding @reboot /bin/startup.sh and having the bash file configured to run the /home/project/script.py添加@reboot /bin/startup.sh并配置 bash 文件以运行/home/project/script.py
  • Using etc/rc.local , etc/init/mystartup.conf , etc/systemd/mystartup.conf使用etc/rc.localetc/init/mystartup.confetc/systemd/mystartup.conf
  • Passing User Data传递用户数据

Probably missing a few others and literally nothing worked even though running the script manually works wonders.可能缺少其他一些,即使手动运行脚本也能产生奇迹,但实际上没有任何效果。

Thanks a lot in advance for the help!非常感谢您的帮助!

Put the script into: /var/lib/cloud/scripts/per-boot/将脚本放入: /var/lib/cloud/scripts/per-boot/

Cloud-Init, which runs User Data, will also check this directory.运行用户数据的 Cloud-Init 也将检查此目录。

From Modules — cloud-init documentation :来自模块 — cloud-init 文档

Any scripts in the scripts/per-boot directory on the datasource will be run every time the system boots.每次系统启动时,数据源上的scripts/per-boot目录中的任何脚本都会运行。 Scripts will be run in alphabetical order.脚本将按字母顺序运行。

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

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