Adding a service startup script for Amazon linux AMI

user915745 picture user915745 · Dec 5, 2011 · Viewed 11.8k times · Source

I am using an Amazon Linux AMI and doing some custom modifications(added an axis2server, etc) on it and saving it as a new AMI. Now what I want to do is when the AMI boots up, start up axis2server(ie.axis2server should automatically start when the instance boots up). For that I used a init script like below and ran the following command:

chkconfig --add axisservice

But when I launch a new instance from my image, the axis2server is not getting started.

I just only need to execute the script /home/ec2-user/axis2-1.6.1/bin/axis2server.sh at startup. Am I missing anything here?

#! /bin/sh
# Basic support for IRIX style chkconfig
###
# chkconfig: 235 98 55
# description: Manages the services you are controlling with the chkconfig command
###

case "$1" in
  start)
        echo -n "Starting axisservice"        
        touch ~/temp.txt
        cd /home/ec2-user/axis2-1.6.1/bin
        ./axis2server.sh &
        echo "."
        ;;
  stop)
        echo -n "Stopping axisservice"
        echo "."
        ;;

  *)
        echo "Usage: /sbin/service axisservice {start|stop}"
        exit 1
esac

exit 0

I went through https://help.ubuntu.com/community/CloudInit as well and it provides a mechanism called User-Data Scripts, where a user can execute a script when launching the script.

$ euca-run-instances --key mykey --user-data-file myscript.sh ami-axxxx

This is a command line option and what I want is something like when I launch the instance through the UI, the script should be started.Therefore, I think the above option can not be used in my case. Please correct me if I am wrong.

Thanks, H.

Answer

Till picture Till · May 24, 2012

I bet the environment is not set(up correctly). This means that I am guessing that your shell script tries to start another program and it's not to be found.

So at first, I'd adjust the start part of your script (current):

echo -n "Starting axisservice"        
touch ~/temp.txt
cd /home/ec2-user/axis2-1.6.1/bin
./axis2server.sh &
echo "."

Edited:

echo -n "Starting axisservice"        
touch ~/temp.txt
cd /home/ec2-user/axis2-1.6.1/bin
./axis2server.sh
RETVAL=$?
[ $RETVAL -eq 0 ] && echo Success
[ $RETVAL -ne 0 ] && echo Failure
echo "."

So what did I do?

  • removed & so script waits for your shell script (axis2server.sh) to complete
  • checked the return status ($?) of your shell script

Further debugging:

Add set -x to your scripts to enable tracing and log both stderr and stdout.

Questions:

  1. Are you are aware that stop (in your service script) doesn't do anything?
  2. touch ~/temp.txt is that supposed to create /root/temp.txt? (I'm guessing root runs this script.)
  3. If none of my suggestions work, can you share axis2server.sh and paste stderr and stdout?