Tuesday, January 3, 2012

Steps for creating automatic startup/shutdown scripts for Tomcat in Amazon EC2

After Tomcat is installed in EC2, it's time to create a startup/shutdown service script. Below are steps:
  1. login to EC2 instance with putty
  2. sudo su
  3. cd /etc/init.d
  4. vim tomcat
  5. put following into the file  
  6. #!/bin/bash
    # chkconfig: 234 20 80
    # description: Tomcat Server basic start/shutdown script
    # processname: tomcat
    JAVA_HOME=/usr/java/jdk1.6.0_24
    export JAVA_HOME
    TOMCAT_HOME=/usr/share/apache-tomcat-7.0.23/bin
    START_TOMCAT=/usr/share/apache-tomcat-7.0.23/bin/startup.sh
    STOP_TOMCAT=/usr/share/apache-tomcat-7.0.23/bin/shutdown.sh
    start() { echo -n "Starting tomcat: "
    cd $TOMCAT_HOME
    ${START_TOMCAT}
    echo "done." }
    stop() { echo -n "Shutting down tomcat: "
    cd $TOMCAT_HOME
    ${STOP_TOMCAT} echo "done." }
    case "$1" in
    start)
    start
    ;;
    stop)
    stop
    ;;
    restart)
    stop
    sleep 10
    start
    ;;
    *)
    echo "Usage: $0 {start|stop|restart}"
    esac
    exit 0
  7. Change permissions.  chmod 755 tomcat
  8. Add script to system services. chkconfig --add tomcat
  9. Test it out. service tomcat start, service tomcat stop.

No comments:

Post a Comment