Creating a script for automatic startup

On a production server, we need to configure Tomcat as the system service, that is, a program running at boot without any users' action. If you install Tomcat on Windows, the installer creates the service for you and sets it for an automatic startup.

Are you wondering how to do it on Linux? In this recipe, we are going to configure your Linux box for automatic start of services. We will create a script and learn how it works.

How to do it…

  1. Open your preferred editor and enter the following lines. Be sure to launch the editor with sudo, as we are going to create a file in a system folder:
    #!/bin/sh
    ### BEGIN INIT INFO
    # Provides:          tomcat
    # Required-Start:    $local_fs $remote_fs $network $syslog
    # Required-Stop:     $local_fs $remote_fs $network $syslog
    # Default-Start:     2 3 4 5
    # Default-Stop:      0 1 6
    # Short-Description: Start/Stop Tomcat  v7.0.42
    ### END INIT INFO
    #
    #  /etc/init.d/tomcat
    #
    export JAVA_HOME=/usr/lib/jvm/jre1.6.0_37
    export PATH=$JAVA_HOME/bin:$PATH
    export CATALINA_HOME=/opt/tomcat7042
    export JAVA_OPTS="-Djava.awt.headless=true"
    
    case $1 in
        start)
            sh $CATALINA_HOME/bin/startup.sh
        ;;
        stop)
            sh $CATALINA_HOME/bin/shutdown.sh
        ;;
        restart)
            sh $CATALINA_HOME/bin/shutdown.sh
            sh $CATALINA_HOME/bin/startup.sh
        ;;
        *)
            echo "Usage: /etc/init.d/tomcat {start|stop|restart}"
            exit 1
        ;;
    esac
    
    exit 0
  2. The preceding script is simple and contains all of the basic elements you will need to get going. Pay attention to the path; you should adjust your script according to your system settings.
  3. Call the new file called tomcat and save it in the /etc/init.d folder.
  4. Now, set the permissions for your script to make it executable:
    $ sudo chmod a+x /etc/init.d/tomcat
    
  5. Let's try to call it and look for any problems:
    $ sudo service tomcat
    Usage: /etc/init.d/tomcat {start|stop|restart}
    
  6. Try starting Tomcat with the following command:
    $ sudo service tomcat start
    
  7. Using the following command lines, check whether Tomcat is running:
    $ ps -ef | grep java
    root      2813     1 99 19:06 pts/0    00:00:24 /usr/bin/java -Djava.util.logging.config.file=/opt/Tomcat7042/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/opt/Tomcat7042/endorsed -classpath /opt/Tomcat7042/bin/bootstrap.jar:/opt/Tomcat7042/bin/tomcat-juli.jar -Dcatalina.base=/opt/Tomcat7042 -Dcatalina.home=/opt/Tomcat7042 -Djava.io.tmpdir=/opt/Tomcat7042/temp org.apache.catalina.startup.Bootstrap start
    
  8. Now we will use the script to stop Tomcat:
    $ sudo service tomcat stop
    
  9. Now that you have a working script, the last step is adding to configured services; we will use update-rc as follows:
    $ sudo update-rc.d tomcat defaults
     Adding system startup for /etc/init.d/tomcat ...
       /etc/rc0.d/K20tomcat -> ../init.d/tomcat
       /etc/rc1.d/K20tomcat -> ../init.d/tomcat
       /etc/rc6.d/K20tomcat -> ../init.d/tomcat
       /etc/rc2.d/S20tomcat -> ../init.d/tomcat
       /etc/rc3.d/S20tomcat -> ../init.d/tomcat
       /etc/rc4.d/S20tomcat -> ../init.d/tomcat
       /etc/rc5.d/S20tomcat -> ../init.d/tomcat
    
  10. Reboot your system and check whether Tomcat is running.

How it works…

We created a shell script to start Apache Tomcat. Now, as you boot your Linux machine, Tomcat will be initialized and all the web application contained will be available for user requests. If you prefer to manually start and stop Tomcat, the script could be useful for you. Just create it as described and avoid the last step. You will use the script to start or stop Tomcat from the command line, that is, sudo service tomcat start or sudo service tomcat stop.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.176.145