官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 聂拉木县| 枣强县| 金山区| 伊吾县| 峨边| 嘉黎县| 土默特左旗| 新源县| 泽普县| 霍州市| 娄烦县| 密云县| 扎赉特旗| 莫力| 重庆市| 栾川县| 晋中市| 灌云县| 万宁市| 深圳市| 历史| 北流市| 沐川县| 临洮县| 阿图什市| 扬中市| 鲁山县| 溧阳市| 勃利县| 岢岚县| 桂平市| 隆子县| 定远县| 泊头市| 灵山县| 临清市| 洛川县| 莒南县| 南投县| 增城市| 九江市|