background

In Linux CentOS7, you can add startup options in either of the following ways: place the script file in the /etc/init.d directory and run the chkconfig command to add startup options. The other is to write a service file that is started by a systemd call.

The MySQL startup service file is mysql.service, which is added in systemd mode.

Our Java Web applications typically rely on MySQL database services, and we must ensure that MySQL services are started before starting the Tomcat container. Therefore, if the Chkconfig mode is used, Tomcat may start before MySQL and the Connection to the Tomcat application database may be abnormal.

The systemd service management command can specify the dependency relationship between services. Based on this, you can customize the tomcat.service file to start Tomcat upon startup.

The first step is to write the startup script

Prepare the Shell script for Tomcat startup and receive startup parameters such as start and stop. It can be done directly by calling Tomcat’s startup.sh, or it can add additional processes, such as Web environment initialization logic, and then call startup.sh.

This is a common Tomcat startup service script

#! /bin/bash JAVA_HOME=/usr/java/jdk1.8.0_151 CATALINA_HOME=/usr/lib/tomcat export JAVA_HOME export CATALINA_HOME start_tomcat=$CATALINA_HOME/bin/startup.sh stop_tomcat=$CATALINA_HOME/bin/shutdown.sh start() { echo -n "Starting tomcat: " ${start_tomcat} echo "Tomcat start ok." } stop() { echo -n "Shutting down tomcat: " ${stop_tomcat} echo "Tomcat stop ok." } case "$1" in start) start ;; stop) stop ;; restart) stop sleep 10 start ;; *) echo "Usage: $0 {start|stop|restart}" esac exit 0Copy the code

Touch /home/tomcat, vi Edit and enter the preceding script content. The startup script is written. Essentially a shell script that calls the actual Tomcat script.

The second step is to write the service file

Upon startup, systemd reads.service files in the /etc/systemd/system directory. Write the service file.

cd /etc/systemd/system 
touch tomcat.service
Copy the code

Write the tomcat.service file as follows

[Unit] Description=MyTomcat After=mysql.service [Service] Type=forking # Disable service start and stop timeout logic of  systemd for mysqld service. TimeoutSec=0 ExecStart=/home/tomcat start ExecStop=/home/tomcat stop PrivateTmp=true [Install] WantedBy=multi-user.targetCopy the code

Matters needing attention:

  1. After in Unit is set to mysql.service to ensure that the mysql service is started before the service script is executed.
  2. TimeoutSec=0 indicates that Tomcat startup timeout exceptions are not handled during startup, which ensures that Tomcat will not be detected by the system if Tomcat takes a long timeterminating.
  3. PID File can be specified as a File under the /run directory, such as /run/mytomcat. PID but should not be used tomcat. PID. A real Tomcat process also has a tomcat.pid process number file and must avoid conflicts.
  4. ExecStart and ExecStop are startup scripts that specify the service, and can be used directly if you do not have the Tomcat script written in the first steptomcat/bin/startup.sh.

Step 3: Start the service

After the tomcat.service file is ready, run the systemctl enable tomcat.service command to add the service.

Systemctl start/stop/status Tomcat. service Starts, stops, and checks the status.

In addition, after the service file is modified, run the systemctl daemon-reload command to reload it again.

The revelation of

First of all, after adding Tomcat in this way, use the JPS command to check the Java process, you can’t get a Tomcat process, can only use ps – ef | grep to check the Java way, this is different from the chkconfig way.

Second, the actual Tomcat script starts with a tomcat.pid process file in the root directory, whereas chkconfig does not.

Third, when chkconfig is added for startup, if the sleep command is executed in the startup script to wait for other services to complete, timeout interrupts startup.

Dec 6 19:17:21 localhost systemd: tomcat.service start operation timed out. Terminating.

Dec 6 19:17:21 localhost systemd: Failed to start SYSV: Starts and Stops the Tomcat daemon.. Dec 6 19:17:21 localhost systemd: Unit tomcat.service entered failed state. Dec 6 19:17:21 localhost systemd: tomcat.service failed.

Fourth, Linux system log file /var/log/messages records all log information about system startup. Generally, service startup operations have pairs of log information. We can check whether the service startup process is correct here.

MySQL > select * from ‘MySQL’;

Starting MySQL Server… Started MySQL Server. Starging MyTomcat… Started MyTomcat

After MyTomcat is configured to start aftemysql.service, the log is in this order.