简体   繁体   English

aws ec2 启动时运行脚本程序

[英]Aws Ec2 run script program at startup

There is a method to setup an EC2 machine to execute Kafka starting script on startup?有没有一种方法可以设置 EC2 机器在启动时执行 Kafka 启动脚本? I use also java Aws SDK, so I accept both solution for a program java that run command on EC2 instance and solutions for a bash script mode that run kafka script at startup.我也使用 java Aws SDK,所以我接受在 EC2 实例上运行命令的程序 java 的解决方案和在启动时运行 kafka 脚本的 bash 脚本模式的解决方案。

A script can be passed in the User Data property.可以在用户数据属性中传递脚本。

If you are using the Amazon Linux AMI, and the first line of the script begins with #!如果您使用的是 Amazon Linux AMI,并且脚本的第一行以#! , then the script will be executed the first time that the instance is started . ,则脚本将在实例第一次启动时执行

For details, see: Running Commands on Your Linux Instance at Launch有关详细信息,请参阅: 启动时在 Linux 实例上运行命令

Adding a script under User Data in CloudFormation only runs once, right when the instance is launched but not when the instance is restarted which is what I needed for my use case.在 CloudFormation 中的用户数据下添加脚本只运行一次,就在实例启动时,而不是在实例重新启动时运行,这是我的用例所需要的。 I use the rc.local approach as commented above and here .我使用上面和这里评论的rc.local方法。 The following in effect appends my script to the rc.local file and performs as expected:以下实际上将我的脚本附加到rc.local文件并按预期执行:

Resources:
  VM:
    Type: 'AWS::EC2::Instance'
    Properties:
      [...]
      UserData:
        'Fn::Base64': !Sub |
          #!/bin/bash -x
          echo 'INSTANCEID="$(curl -s http://169.254.169.254/latest/meta-data/instance-id)"' >> /etc/rc.local
          #echo 'INSTANCEID=$(ls /var/lib/cloud/instances)' >> /etc/rc.local
          echo 'echo "aws ec2 stop-instances --instance-ids $INSTANCEID --region ${AWS::Region}" | at now + ${Lifetime} minutes' >> /etc/rc.local

Additional tip : You can inspect the user data (the current script) and modify it using the AWS console by following these instructions: View and update the instance user data .附加提示:您可以按照以下说明检查用户数据(当前脚本)并使用 AWS 控制台对其进行修改: 查看和更新​​实例用户数据

What is the OS of EC2 instance? EC2实例的操作系统是什么?

  1. You could use userdata script at instance launch time.您可以在实例启动时使用 userdata 脚本。 Remember this is just 1time activity请记住,这只是 1 次活动

  2. If your requirement is to start the script everytime you reboot EC2 instance then you could make use of rc.local file on Linux instances which is loaded at OS boot time.如果您的要求是每次重启 EC2 实例时都启动脚本,那么您可以使用 Linux 实例上的rc.local文件,该文件在操作系统启动时加载。

rc.local didn't work for me. rc.local 对我不起作用。

I used crontab following this guide, which sets up a cron job that can run a script on start up.我按照本指南使用了 crontab,它设置了一个可以在启动时运行脚本的 cron 作业。

https://phoenixnap.com/kb/crontab-reboot https://phoenixnap.com/kb/crontab-reboot

It's essentially它本质上是

crontab -e
<select editor>
@reboot <script to run>

If you are running a windows EC2 you'll want to read: https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2-windows-user-data.html如果您运行的是 Windows EC2,您需要阅读: https : //docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2-windows-user-data.html

Example:示例:

<script>
echo Current date and time >> %SystemRoot%\Temp\test.log
echo %DATE% %TIME% >> %SystemRoot%\Temp\test.log
</script>

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

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