2012年1月25日星期三

Do It Scared! » Blog Archive » Installing Tomcat as a Linux Service

Do It Scared! » Blog Archive » Installing Tomcat as a Linux Service

Now that Tomcat is running successfully, you may wish to configure Tomcat to run on boot. One way of doing so is to configure Tomcat to be a service and registered it to be launched as part of the boot.

  1. First, create the shell script file below and name it “tomcat” (or whatever service name you want to use).
    #!/bin/sh
    #
    # /etc/init.d/tomcat
    #
    # This is the init script for starting up the
    # Jakarta Tomcat server
    #
    # description: Starts and stops the Tomcat daemon.
    #

    tomcat=/opt/apache-tomcat-6.0.18
    startup=$tomcat/bin/startup.sh
    shutdown=$tomcat/bin/shutdown.sh

    start() {
    echo -n $"Starting Tomcat service: "
    su - tomcat -c $startup
    echo $?
    }

    stop() {
    echo -n $"Stopping Tomcat service: "
    su - tomcat -c $shutdown
    echo $?
    }

    restart() {
    stop
    start
    }

    status() {
    ps -aef | grep apache-tomcat | grep -v tomcat6 | grep -v grep
    }

    # Handle the different input options
    case "$1" in
    start)
    start
    ;;
    stop)
    stop
    ;;
    status)
    status
    ;;
    restart)
    restart
    ;;
    *)
    echo $"Usage: $0 {start|stop|restart|status}"
    exit 1
    esac

    exit 0
  2. Note that when you run a process as a service, its environment may be different from the environment you see when you log in.
    • Under SUSE Linux, you may wish to add the line below to the top of the tomcat script to ensure that the tomcat process will run with the environmental variables that you have set in “/etc/profile”.
      source /etc/profile
    • To see what environment a process is running under, you can use the following commands:
      ps -aef | grep tomcat
      ... you will get a process id number like 5054 ...
      cat /proc/5054/environ
  3. Copy the file to the “/etc/init.d” directory (example: “/etc/init.d/tomcat”).
  4. Give it execute permission:
    cd /etc/init.d
    chmod u+x tomcat
  5. You can test the script by running this command (which will return the Tomcat process info if it is running; otherwise, it will return blank):
    cd /etc/init.d
    ./tomcat status
  6. Add tomcat as a service by running the following:
    chkconfig --add tomcat
  7. Now that tomcat is a service, you can issue these commands from anywhere:
    service tomcat start
    service tomcat stop
    service tomcat restart
    service tomcat status
  8. Set the tomcat service to start at boot:
    chkconfig tomcat on

没有评论: