repost.aws Open in urlscan Pro
13.224.189.55  Public Scan

URL: https://repost.aws/knowledge-center/execute-user-data-ec2
Submission: On June 18 via api from US — Scanned from DE

Form analysis 1 forms found in the DOM

<form><label for="CommentCreate_md" class="PostBody_label__pfzPS">Comment on this article</label>
  <div class="Editor_wrapper__DGWud">
    <div class="Editor_editor__Zwi9K PostBody_editor__uVYDf">
      <div id="CommentCreate" class="rc-md-editor  ">
        <div class="rc-md-navigation visible">
          <div class="navigation-nav left">
            <div class="button-wrap"><span class="button button-type-bold" title="Bold" role="button" tabindex="0"><i class="rmel-iconfont rmel-icon-bold"></i></span><span class="button button-type-italic" title="Italic" role="button"
                tabindex="0"><i class="rmel-iconfont rmel-icon-italic"></i></span><span class="button button-type-strikethrough" title="Strikethrough" role="button" tabindex="0"><i class="rmel-iconfont rmel-icon-strikethrough"></i></span><span
                class="button button-type-unordered" title="Unordered List" role="button" tabindex="0"><i class="rmel-iconfont rmel-icon-list-unordered"></i></span><span class="button button-type-order" title="Ordered List" role="button"
                tabindex="0"><i class="rmel-iconfont rmel-icon-list-ordered"></i></span><span class="button button-type-block-quote" title="Quote" role="button" tabindex="0"><i class="rmel-iconfont rmel-icon-quote"></i></span><span
                class="button button-type-block-code-block" title="Code" role="button" tabindex="0"><i class="rmel-iconfont rmel-icon-code-block"></i></span><span class="button button-type-code-inline" title="Inline Code" role="button"
                tabindex="0"><i class="rmel-iconfont rmel-icon-code"></i></span><span class="button button-type-table" title="Table"><i class="rmel-iconfont rmel-icon-grid"></i>
                <div class="drop-wrap hidden">
                  <ul class="table-list wrap" style="width: 135px; height: 112px;">
                    <li class="list-item " style="top: 0px; left: 0px;"></li>
                    <li class="list-item " style="top: 0px; left: 23px;"></li>
                    <li class="list-item " style="top: 0px; left: 46px;"></li>
                    <li class="list-item " style="top: 0px; left: 69px;"></li>
                    <li class="list-item " style="top: 0px; left: 92px;"></li>
                    <li class="list-item " style="top: 0px; left: 115px;"></li>
                    <li class="list-item " style="top: 23px; left: 0px;"></li>
                    <li class="list-item " style="top: 23px; left: 23px;"></li>
                    <li class="list-item " style="top: 23px; left: 46px;"></li>
                    <li class="list-item " style="top: 23px; left: 69px;"></li>
                    <li class="list-item " style="top: 23px; left: 92px;"></li>
                    <li class="list-item " style="top: 23px; left: 115px;"></li>
                    <li class="list-item " style="top: 46px; left: 0px;"></li>
                    <li class="list-item " style="top: 46px; left: 23px;"></li>
                    <li class="list-item " style="top: 46px; left: 46px;"></li>
                    <li class="list-item " style="top: 46px; left: 69px;"></li>
                    <li class="list-item " style="top: 46px; left: 92px;"></li>
                    <li class="list-item " style="top: 46px; left: 115px;"></li>
                    <li class="list-item " style="top: 69px; left: 0px;"></li>
                    <li class="list-item " style="top: 69px; left: 23px;"></li>
                    <li class="list-item " style="top: 69px; left: 46px;"></li>
                    <li class="list-item " style="top: 69px; left: 69px;"></li>
                    <li class="list-item " style="top: 69px; left: 92px;"></li>
                    <li class="list-item " style="top: 69px; left: 115px;"></li>
                    <li class="list-item " style="top: 92px; left: 0px;"></li>
                    <li class="list-item " style="top: 92px; left: 23px;"></li>
                    <li class="list-item " style="top: 92px; left: 46px;"></li>
                    <li class="list-item " style="top: 92px; left: 69px;"></li>
                    <li class="list-item " style="top: 92px; left: 92px;"></li>
                    <li class="list-item " style="top: 92px; left: 115px;"></li>
                  </ul>
                </div>
              </span><span class="button button-type-link" title="Link" role="button" tabindex="0"><i class="rmel-iconfont rmel-icon-link"></i></span><span class="button button-type-undo disabled" title="Undo"
                tabindex="-1"><i class="rmel-iconfont rmel-icon-undo"></i></span><span class="button button-type-redo disabled" title="Redo" tabindex="-1"><i class="rmel-iconfont rmel-icon-redo"></i></span><span></span></div>
          </div>
          <div class="navigation-nav right">
            <div class="button-wrap">
              <span><a title="Preview" role="button" tabindex="0">Preview</a></span><span><a title="Formatting guide" href="https://www.markdownguide.org/basic-syntax/" target="_blank" rel="noopener noreferrer">Formatting guide</a></span></div>
          </div>
        </div>
        <div class="editor-container">
          <section class="section sec-md visible"><textarea id="CommentCreate_md" name="textarea" placeholder="Start writing your comment" class="section-container input " wrap="hard" maxlength="15000" aria-invalid="false"
              aria-describedby="CommentCreate-error" style="height: 200px;"></textarea></section>
          <section class="section sec-html visible">
            <div id="CommentCreate_html" class="section-container html-wrap" style="height: 200px;">
              <div class="custom-html-style"></div>
            </div>
          </section>
        </div>
      </div>
    </div>
  </div>
  <div id="CommentCreate-error" class="PostBody_errors__ylDGI"></div>
  <div class="ReplyCreate_actionContainer__Hqd27"><button title="Clear" type="button" class="ant-btn ant-btn-default Button_secondary__pFIlL Button_disabled__Cu6k9 Button_large__AqiVe " disabled=""><span>Clear</span></button><span
      style="display:inline-block;cursor:not-allowed" class="ant-tooltip-disabled-compatible-wrapper"><button title="Post comment" style="pointer-events:none" type="button"
        class="ant-btn ant-btn-default Button_primary__swzAa Button_disabled__Cu6k9 Button_large__AqiVe " disabled=""><span>Post comment</span></button></span></div>
</form>

Text Content

SELECT YOUR COOKIE PREFERENCES

We use essential cookies and similar tools that are necessary to provide our
site and services. We use performance cookies to collect anonymous statistics so
we can understand how customers use our site and make improvements. Essential
cookies cannot be deactivated, but you can click “Customize cookies” to decline
performance cookies.

If you agree, AWS and approved third parties will also use cookies to provide
useful site features, remember your preferences, and display relevant content,
including relevant advertising. To continue without accepting these cookies,
click “Continue without accepting.” To make more detailed choices or learn more,
click “Customize cookies.”

Accept all cookiesContinue without acceptingCustomize cookies


CUSTOMIZE COOKIE PREFERENCES

We use cookies and similar tools (collectively, "cookies") for the following
purposes.


ESSENTIAL

Essential cookies are necessary to provide our site and services and cannot be
deactivated. They are usually set in response to your actions on the site, such
as setting your privacy preferences, signing in, or filling in forms.




PERFORMANCE

Performance cookies provide anonymous statistics about how customers navigate
our site so we can improve site experience and performance. Approved third
parties may perform analytics on our behalf, but they cannot use the data for
their own purposes.

Allow performance category
Allowed


FUNCTIONAL

Functional cookies help us provide useful site features, remember your
preferences, and display relevant content. Approved third parties may set these
cookies to provide certain site features. If you do not allow these cookies,
then some or all of these services may not function properly.

Allow functional category
Allowed


ADVERTISING

Advertising cookies may be set through our site by us or our advertising
partners and help us deliver relevant marketing content. If you do not allow
these cookies, you will experience less relevant advertising.

Allow advertising category
Allowed

Blocking some types of cookies may impact your experience of our sites. You may
review and change your choices at any time by clicking Cookie preferences in the
footer of this site. We and selected third-parties use cookies or similar
technologies as specified in the AWS Cookie Notice.

CancelSave preferences




UNABLE TO SAVE COOKIE PREFERENCES

We will only store essential cookies at this time, because we were unable to
save your cookie preferences.

If you want to change your cookie preferences, try again later using the link in
the AWS console footer, or contact support if the problem persists.

Dismiss


By using AWS re:Post, you agree to the AWS re:Post Terms of Use
re:Post
Search

English
EnglishDeutschEspañolFrançaisItaliano日本語한국어Português中文 (简体)中文 (繁體)
Resources
Sign in
 * Home
 * Questions
 * Knowledge Center
 * Community Articles
 * Selections
 * Tags
 * Topics
 * More…

Ask question

AWS Business Support. See Eligibility for a free 60-day trial

Less than 1 hour response times for production system down cases, 24x7 access to
cloud experts and 400+ personalized recommendations to optimize costs, improve
performance, increase security, and monitor service quotas with Trusted Advisor.
Click here to see if you are eligible for a free 60-day trial.


 1. /
 2. Knowledge Center/
 3. How can I utilize user data to automatically run a script with every restart
    of my Amazon EC2 Linux instance?/


HOW CAN I UTILIZE USER DATA TO AUTOMATICALLY RUN A SCRIPT WITH EVERY RESTART OF
MY AMAZON EC2 LINUX INSTANCE?

3 minute read
2

I want to utilize user data to run a script every time my Amazon Elastic Compute
Cloud (Amazon EC2) instance is restarted. How can I do that?


SHORT DESCRIPTION

By default, user data scripts and cloud-init directives run only during the
first boot cycle when an EC2 instance is launched. However, you can configure
your user data script and cloud-init directives with a mime multi-part file. A
mime multi-part file allows your script to override how frequently user data is
run in the cloud-init package. Then, the file runs the user script. For more
information on mime multi-part files, see Mime Multi Part Archive on the
cloud-init website.

Note: It's a best practice to create a snapshot of your instance before
proceeding with the following resolution.


RESOLUTION

Warning: Before starting this procedure, review the following:

 * Stopping and starting the instance erases any data on instance store volumes.
   Be sure that you back up any data on the instance store volumes that you want
   to keep. For more information, see Determine the root device type of your
   AMI.
 * If your EC2 instance has an auto-assigned public IPv4 address, then stopping
   and starting the instance causes the IPv4 address to change. If you need the
   instance to have a static public IPv4 address, consider using an Elastic IP
   address. Elastic IP addresses persist after an instance stop and start.

1.    Make sure that the latest version of cloud-init is installed and
functioning properly on your EC2 instance.

2.    For security reasons, create an IAM policy to restrict the users who are
allowed to add or remove user data through the ModifyInstanceAttribute API.

3.    Open the Amazon EC2 console.

4.    Stop your instance.

5.    Choose Actions, choose Instance Settings, and then choose Edit User Data.

6.    Copy your user script into the Edit user data box, and then choose Save.

The following example is a shell script that writes "Hello World" to a
testfile.txt file in a /tmp directory.

Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0

--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"

#cloud-config
cloud_final_modules:
- [scripts-user, always]

--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"

#!/bin/bash
/bin/echo "Hello World" >> /tmp/testfile.txt
--//--

By default, cloud-init allows only one content type in user data at a time.
However, this example shows both text/cloud-config and text/x-shellscript
content-types in a mime-multi part file.

The text/cloud-config content type overrides how frequently user data is run in
the cloud-init package by setting the SCRIPTS-USER parameter to ALWAYS.

The text/x-shellscript content type provides the actual user script to be run by
the cloud-init cloud_final_modules module. In this example, there is only one
line to be run, which is /bin/echo "Hello World." >> /tmp/testfile.txt.

Note: Replace the line /bin/echo "Hello World." >> /tmp/testfile.txt with the
shell script that you want to run during the instance boot.

7.    Start your EC2 instance again, and validate that your script runs
correctly.

--------------------------------------------------------------------------------


RELATED INFORMATION

Run commands on your Linux instance at launch

Run commands on your Windows instance at launch

AWS Systems Manager Automation

User-Data Formats

FollowShare
Topics
Compute
Tags
Amazon EC2Linux
Language
English


RELATED VIDEOS

Watch Tanvi’s video to learn more (3:15)
AWS OFFICIALUpdated 2 years ago
6 Comments

To run a script on every boot of an instance, place the script into this
directory:

/var/lib/cloud/scripts/per-boot/

Amazon Linux uses Cloud-Init to run User Data on first boot. It will also run
any scripts in the above directory after EVERY boot.

Share
John Rotenstein
replied a year ago

Thank you for your comment. We'll review and update the Knowledge Center article
as needed.

Share
MODERATOR
AWS Official
replied a year ago

#!/bin/bash /bin/echo "Hello World" >> /tmp/testfile.txt

where this content is written inside ec2 ?

Share
punit
replied a year ago

Thank you for your comment. We'll review and update the Knowledge Center article
as needed.

Share
MODERATOR
AWS Official
replied a year ago

I am trying to run a jar file while starting the EC2 instance.

java -jar /home/ec2-user/springboot.0.0.1-SNAPSHOT.jar

I ssh into EC2 and lsof -i:8090 it does not show any pid.

How can I make sure that the spring boot application is running on port 8090
(logs?)

Share
lavanya
replied a year ago

Thank you for your comment. We'll review and update the Knowledge Center article
as needed.

Share
MODERATOR
AWS Official
replied a year ago
Comment on this article
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 

PreviewFormatting guide


ClearPost comment


RELEVANT CONTENT

 * EC2 user data script failure alert
   Himanshu Bansal
   asked a month ago
 * Run shell script as EC2 user on Cloud formation User data
   AWS-User-4586895
   asked 2 years ago
 * Run shell script as EC2 user on Cloud formation User data
   AWS-User-4586895
   asked 2 years ago
 * Unexpected instance reboots following cloud-init user data script termination
   tpetillot
   asked 4 months ago
 * How to call long-running User Data Script with parameters from Cloud
   Formation?
   rePost-User-2002390
   asked 2 years ago
 * How can I troubleshoot issues when I run user data scripts to configure my
   EC2 Windows instance?
   AWS OFFICIALUpdated 10 months ago
 * How do I use EC2Launch to run a user data script on my EC2 Windows instance?
   AWS OFFICIALUpdated 10 months ago
 * How can I send user-data output to the console logs on an EC2 instance for
   Amazon Linux 1, Amazon Linux 2, or Amazon Linux 2023?
   AWS OFFICIALUpdated 2 years ago
 * How can I use user data to add a new user account with RDP access to my
   Amazon EC2 instance?
   AWS OFFICIALUpdated 5 months ago
 * Annoucing AWS analytics services streamline user access to data, permissions
   setting, and auditing
   EXPERT
   A_J
   published 19 days ago


FEEDBACK
 * Privacy|
 * Site Terms|
 * Cookie Preferences|
 * Sitemap|
 * Legal|
 * © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved.

Community Guidelines