Linux – Run a bash script after EC2 instance boots

amazon ec2bashbootlinux

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!

Best Answer

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.