I have a script on an EC2 instance that remotely starts another instance.

Once this instance has fully loaded (finished booting) I want it to automatically run a bash script, what would be the best way to do this?

I need everything to be fully started, basically the bash script runs a image conversion script (using ImageMagick and executes the "wget" command a few times)

Currently, the script is located here:


And I can start it manually by executing

bash beginProcess.sh


Also there is an EBS volume attached to this, if that helps, Thanks!

4 Answers 4


I'd suggest just using the user-data option to ec2-run-instances. It lets you give a script of some sort to the VM which will be run on first boot. If you're using ubuntu or debian, you can use cloud-init, which puts some nice polish on the process. If using cloud-init, you can use the [runcmd] section of the config file to specify arbitrary commands to run after boot.

Thanks to SF user Eric Hammond for the user-data page. Check out his site - it has a wealth of information on AWS.

Edit: After re-reading, it's not clear whether you wanted to run a command on initial boot or on every boot. The above instructions only apply to the initial boot. If you want to run a command on every boot, you have a couple options - you can run a command via the @reboot cron directive, or alternatively you can add the script to /etc/rc.local, which will be run each time the system boots.

  • 2
    Awesome thanks for the response, to clarify I wanted it to run each time the system boots, /etc/rc.local is working wonderfully, cheers
    – SSH This
    Mar 15, 2012 at 15:20

If you were using an AMI that had the cloud-init package installed (like Amazon Linux or Ubuntu) then you could simply pass the bash script (which starts with #!) as the user-data-file and it would run automatically at the end of the boot process.

For example, it could be as simple as:

ec2-run-instances                             \
  --user-data-file /home/root/beginProcess.sh \
  --key $USER                                 \

Here's the article where I introduced the user-data script concept, now available in major EC2 AMIs like Amazon Linux and Ubuntu: http://alestic.com/2009/06/ec2-user-data-scripts

Unfortunately, it looks like you're wanting to use a RHEL AMI. I ran a copy of that and could not find any references to cloud-init or running user-data scripts on first boot, nor did a test of the same work.

I'm not saying you should switch Linux distros just for this, but Amazon Linux is based on RHEL, so that might work for you.

Here's an article I wrote that might help you debug your user-data script if it doesn't work the first time: http://alestic.com/2010/12/ec2-user-data-output

  • Offtopic, but thank you for creating such an invaluable too. Three cheers. Sep 13, 2014 at 17:17

This is an old question but I could not find a good answer to this anywhere. My use case was needing to re-run some commands on an existing instance due to the instance being shut down and started up again on a schedule.

The typical answer is to use EC2 User Data, but this doesn't seem to work for me. Either it's because User Data is only for running commands on newly created instances (such as bootstrapping instances for scaling), or because the documentation is out of date. Not sure which, but I was having no luck getting anything to work. There is apparently some method for running commands each time an existing instance starts shown here: How to make EC2 user data script run again on startup?, but this simply doesn't work for me at all. Nothing happens.

After bumbling around for a while, I took a shot in the dark and just modified the .bashrc file on my EC2 instance, and lo and behold, it works exactly as expected and runs my commands on instance startup. This is almost too obvious, but it's a little remarkable it's not mentioned anywhere.


The authoritative source for how to use EC2 UserData to run a script each time your instance boots is here. In short, I create a script on the instance (make sure to make it executable), then I use the following userdata script:

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"

- [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"


You only need to change the second-to-last line to point to your script.

Another trick I found is that if you were already playing around with this and had some other UserData script on the instance, it is cached on the machine and does not update when you change the UserData script. To force the new UserData script to actually be used, you need to run cloud-init clean on the instance to clear the UserData cache.

  • Nice to have an official answer from AWS! There are so many differing approaches proposed in various forums Nov 11, 2020 at 19:16
  • What would be the file extension for mime type script and how do we make it executable?
    – Brooke
    Dec 9, 2020 at 14:31
  • @Brooke I set this UserData script via the AWS Console web API, not as a file on the instance. The only thing you need to put on the instance is the /path/to/script.sh (and probably make it executable).
    – crockeea
    Dec 9, 2020 at 19:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .