Start tomcat on boot in Linux

As you all know, all startup scripts are found in /etc/init.d folder. To start tomcat on linux boot then below are primary steps:

  1. create tomcat script that starts,stop,restarts tomcat
  2. create symbolic link of tomcat in rc.d folders. Linux system will run all scripts defined in rc.d folders

Create Tomcat Auto startup script:

  1. login with root user
  2. create a file name with tomcat in /etc/init.d create tomcat startup script
    1. cd /etc/init.d
      vi tomcat
  3. if you set the JAVA_HOME and CATALINA_HOME is bash_profile then you no need to set in /etc/init.d/tomcat script
  4. tomcat script is:
    1. ###export CATALINA_HOME="/usr/local/apache-tomcat-7.0.61"
      ## export JAVA_HOME="/usr/local/jdk7"
      export CATALINA_HOME="/usr/local/apache-tomcat-7.0.61"
      ERROR=0
      case "$1" in
       start)
                  echo $"Starting Tomcat"
                  sh $CATALINA_HOME/bin/startup.sh
                  ;;
       stop)
                 echo $"Stopping Tomcat"
                 sh $CATALINA_HOME/bin/shutdown.sh
                 ;;
       restart)
                 sh $CATALINA_HOME/bin/shutdown.sh
                 sh $CATALINA_HOME/bin/startup.sh
                  ;;
       *)
               echo $"Usage: $0 {start|stop|restart}"
       exit 1
       ;;
      esac
      
      exit $ERROR
  5.  chmod 775 tomcat
  6. create symbolic link of the tomcat script in rc.d directory

Add Tomcat script to rc.d directory

We need to create two symbolic links

  • K{level}tomcat    –  K76tomcat  means it kills  the process after K75
  • S{level}tomcat    –  S98tomcat means it starts the tomcat after S97.

Run the below Symbolic commands in Terminal:

  • ln -s /etc/init.d/tomcat /etc/rc.d/rc6.d/K26tomcat tomcat auto startup
  • ln -s /etc/init.d/tomcat /etc/rc.d/rc0.d/K26tomcattomcat auto startup
  • ln -s /etc/init.d/tomcat /etc/rc.d/rc3.d/S81tomcattomcat auto startup
  • ln -s /etc/init.d/tomcat /etc/rc.d/rc2.d/S81tomcat
  • ln -s /etc/init.d/tomcat /etc/rc.d/rc5.d/S81tomcat

I used to prefer to start the tomcat after mysql start.

What are the rc.d directories?

you can found below list of directories in /etc/rc.d and represents  run levels with different modes of operating system

  • /etc/rc.d/rc0.d    –    0   –  HALT or shutdown the system –   All symlinks with K extensions must go here
  • /etc/rc.d/rc1.d   –     1   –  Single mode system
  • /etc/rc.d/rc2.d  –      2   –   Multi user mode with out networking
  • ,/etc/rc.d/rc3.d  –     3   –   Multi user mode with networking enabled. most of the all startup scripts will go here.
  • /etc/rc.d/rc4.d   –     4  –    Not used
  • /etc/rc.d/rc5.d   –     5  –    GUI enabled and starts system with normal mode. Add startup scripts in this.
  • /etc/rc.d/rc6.d   –     6  –  Reboots the system and most scripts start with K level goes into this folder to kill the process

We need to  create Symbolic links

  • K{level}tomcat – create in rc0d, rc6.d
  • S{level}tomcat  – Create in rc3.d,rc5,d,rc2.d

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *