简体   繁体   中英

Aws Ec2 run script program at startup

There is a method to setup an EC2 machine to execute Kafka starting script on startup? 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.

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 #! , 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

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. I use the rc.local approach as commented above and here . The following in effect appends my script to the rc.local file and performs as expected:

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 .

What is the OS of EC2 instance?

  1. You could use userdata script at instance launch time. Remember this is just 1time activity

  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.

rc.local didn't work for me.

I used crontab following this guide, which sets up a cron job that can run a script on start up.

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

Example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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