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

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.

主站蜘蛛池模板: 新余市| 莱阳市| 苗栗县| 延川县| 密山市| 黄平县| 通渭县| 大城县| 吉首市| 大理市| 略阳县| 荆门市| 荥阳市| 安庆市| 平和县| 抚顺县| 奉化市| 徐州市| 化德县| 务川| 冕宁县| 保康县| 偏关县| 齐齐哈尔市| 浠水县| 大港区| 师宗县| 江达县| 九江县| 宜君县| 珲春市| 安西县| 杨浦区| 洛阳市| 股票| 枣庄市| 永州市| 景谷| 本溪市| 肇州县| 义乌市|