Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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!

share|improve this question

2 Answers 2

up vote 13 down vote accepted

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.

share|improve this answer
1  
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 '12 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                                 \
  ami-XXXXXXXX

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

share|improve this answer
    
Offtopic, but thank you for creating such an invaluable too. Three cheers. –  danieljimenez Sep 13 at 17:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.