How to Automatically Execute a Bash Script After EC2 Instance Boot Completion on RHEL 6.2


2 views

When working with AWS EC2 instances, particularly when automating workflows, we often need to execute scripts immediately after instance initialization. In your case, you want beginProcess.sh to run automatically after all system services are fully operational on your RHEL 6.2 instance.

RHEL 6 uses the traditional System V init system, which means we need to create an init script or leverage existing boot mechanisms. The key is ensuring our script runs after all essential services (like networking and EBS mounting) are ready.

Option 1: Using rc.local

The simplest approach for RHEL 6.2 is using /etc/rc.local, which executes after all other init scripts:

#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own commands here if you don't want to do the full
# Sys V style init stuff.

/bin/bash /home/root/beginProcess.sh &> /var/log/beginProcess.log

exit 0

Make it executable:

chmod +x /etc/rc.local

Option 2: Creating a Custom Init Script

For more control, create a proper init script in /etc/init.d/:

#!/bin/bash
#
# chkconfig: 345 99 10
# description: Auto-start beginProcess

case "$1" in
  start)
    echo "Starting beginProcess" >> /var/log/beginProcess.log
    /bin/bash /home/root/beginProcess.sh &>> /var/log/beginProcess.log
    ;;
  *)
    echo "Usage: $0 {start}"
    exit 1
    ;;
esac

exit 0

Then register it:

chmod +x /etc/init.d/beginProcess
chkconfig --add beginProcess
chkconfig beginProcess on

Option 3: Using Cloud-Init

If you're launching from an AMI, consider using cloud-init user data:

#cloud-config
runcmd:
  - [ bash, /home/root/beginProcess.sh ]

Since your script uses ImageMagick and wget, you might want to add explicit checks:

#!/bin/bash

# Wait for network connectivity
while ! ping -c1 google.com &>/dev/null; do
    sleep 2
done

# Check EBS mount
while [ ! -d /path/to/ebs/mount ]; do
    sleep 2
done

# Main script execution
convert input.jpg output.png
wget http://example.com/file1 -O /path/to/download
wget http://example.com/file2 -O /path/to/download

Always implement proper logging for debugging:

exec > >(tee /var/log/beginProcess-$(date +%F).log)
exec 2>&1
echo "$(date) - Script started"
  • Check /var/log/boot.log for boot sequence issues
  • Verify script permissions: chmod +x /home/root/beginProcess.sh
  • Test scripts manually before automating
  • Monitor /var/log/messages for errors

When dealing with EC2 instances, we often need to run post-boot scripts after all system services are fully initialized. The challenge is ensuring the script executes only when the system reaches a stable state, particularly for operations like image processing with ImageMagick or network-dependent tasks using wget.

For RHEL 6.2 (which uses Upstart), we have several reliable methods:


# Method 1: Using rc.local (simplest approach)
sudo nano /etc/rc.local

Add this line before the "exit 0":


su - root -c "/home/root/beginProcess.sh &"

For more control over execution timing:


# Create new Upstart config
sudo nano /etc/init/beginProcess.conf

Add this configuration:


description "Run image processing after boot"
author "Your Name"

start on stopped rc RUNLEVEL=[2345]

script
    # Wait for network availability
    while ! ping -c1 google.com &/dev/null; do
        sleep 2
    done
    
    # Execute script as root
    /bin/bash /home/root/beginProcess.sh
end script

For scripts requiring specific services (like networking):


# Check if NetworkManager is running
start on started NetworkManager

# Alternative for basic network connectivity
start on net-device-up IFACE=eth0

If your script depends on mounted EBS volumes:


start on stopped volmountd

Or explicitly check in your script:


until [ -d /your/ebs/mountpoint ]; do
    sleep 5
done

Always log your script output for debugging:


exec > /var/log/beginProcess.log 2>&1

Check boot logs for execution timing:


grep 'beginProcess' /var/log/boot.log

For newer AMIs, consider using cloud-init:


#cloud-config
runcmd:
  - [ bash, /home/root/beginProcess.sh ]

Remember to make your script executable:


chmod +x /home/root/beginProcess.sh