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:
/home/root/beginProcess.sh
And I can start it manually by executing
bash beginProcess.sh
RHEL-6.2-Starter-EBS-i386
Also there is an EBS volume attached to this, if that helps, Thanks!
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.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:
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
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:
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.