GeoServer Beginner's Guide(Second Edition)
上QQ阅读APP看书,第一时间看更新

Configuring Tomcat as a service on Linux Mint

When installing on Windows, the setup configures Tomcat as a system service. This way, it will start when the computer boots without any user action. Are you wondering why do you have to manually start and stop Tomcat on Linux? You do not. As for Windows, the Linux operating system can be configured for an automatic start of programs.

In this section, you will create a script and learn how it works:

  1. Download the tomcat file from the Packt site for this book and save it on your machine. You have to move it to the /etc/init.d folder.
  2. Open it with the vi editor as follows:
        ~ $ sudo vi /etc/init.d/tomcat 
  1. There are some key settings you need to check in this file. The following lines contain the location of JRE and Tomcat. You can modify them according to your environment:
        export JAVA_HOME=/opt/java/jre1.8.0_121 
        export PATH=$JAVA_HOME/bin:$PATH 
        export CATALINA_HOME=/opt/apache-tomcat-8.5.13 
  1. Now, set the permissions for your script to make it executable using the following:
       ~ $ sudo chmod a+x /etc/init.d/tomcat  
  1. Let's try to call it and check for any problems:
       ~ $ sudo service tomcat
       Usage: /etc/init.d/tomcat {start|stop|restart}
  1. Try starting Tomcat by:
      ~ $ sudo service tomcat start  
  1. To check if Tomcat started without errors, you can open the home page with your browser, as you did in a previous example, or you can check for the process to be running. For this second way, you have to use a ps shell command that returns the list of all the processes currently running on your machine. Piping the list to grep and searching for the Java string, you will find the Tomcat instance:
       ~ $ ps -ef | grep java
       root      1447     1  4 18:18 ?        00:01:21 /usr/bin/java - 
Djava.util.logging.config.file=/opt/apache-tomcat-8.5.5/conf/
logging.properties -Djava.util.logging.manager=org.apache.juli.
ClassLoaderLogManager -Djava.awt.headless=true -Xms1536m
-Xmx1536m -XX:PermSize=256m -XX:MaxPermSize=256m
-Djdk.tls.ephemeralDHKeySize=2048 -classpath /opt/apache-
tomcat-8.5.5/bin/bootstrap.jar:/opt/apache-tomcat-8.5.5/
bin/tomcat-juli.jar -Dcatalina.base=/opt/apache-
tomcat-8.5.5 -Dcatalina.home=/opt/apache-tomcat-8.5.5
-Djava.io.tmpdir=/opt/apache-tomcat-8.5.5/temp
org.apache.catalina.startup.Bootstrap start
  1. We have successfully created a script to start and stop Tomcat, but this still requires you to run it manually from the command shell. For an automatic startup, the last step is adding it to configured services. We will use a system utility, update-rc, to do this:
       ~ $ sudo update-rc.d tomcat defaults
  1. The previous command creates a set of files in some special folders. Each time your operating system changes its run level, for example, when you boot or halt it, the script contained in these folders is executed, so Tomcat will be started or killed. You can take a look at these files:
      ~ $ ls -l /etc/rc?.d/*tomcat
      lrwxrwxrwx 1 root root 16 Oct  6 16:18 /etc/rc0.d/K01tomcat
-> ../init.d/tomcat
lrwxrwxrwx 1 root root 16 Oct 6 16:18 /etc/rc1.d/K01tomcat
-> ../init.d/tomcat
lrwxrwxrwx 1 root root 16 Oct 6 16:18 /etc/rc2.d/S02tomcat
-> ../init.d/tomcat
lrwxrwxrwx 1 root root 16 Oct 6 16:18 /etc/rc3.d/S02tomcat
-> ../init.d/tomcat
lrwxrwxrwx 1 root root 16 Oct 6 16:18 /etc/rc4.d/S02tomcat
-> ../init.d/tomcat
lrwxrwxrwx 1 root root 16 Oct 6 16:18 /etc/rc5.d/S02tomcat
-> ../init.d/tomcat
lrwxrwxrwx 1 root root 16 Oct 6 16:18 /etc/rc6.d/K01tomcat
-> ../init.d/tomcat

We created a shell script to start Apache Tomcat. Now, when you boot your Linux machine, Tomcat will automatically start and all the web application content will be available for user requests. If you prefer to manually start and stop Tomcat, the script could yet 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 tomcat start or sudo tomcat stop.