Very practical Tomcat startup script implementation method

Very practical Tomcat startup script implementation method

Preface

There is a scenario where, for the sake of security, the company needs to impose security restrictions on all logins to the Linux server, requiring that except for the administrator, other employees who want to log in to the Linux server cannot log in with the highest privilege account. New users need to be created, directory and file permissions need to be controlled, and only read, write, and execute permissions are allowed for the directories that need to be operated. Other directories only have read permissions. In addition, all tomcats cannot be started and stopped directly in the bin using startup.sh and shutdown.sh. This operation must be performed by writing a shell script. In other words, there are two steps: creating a user and setting permissions, and writing a tomcat startup script. Let's complete these two steps below.

1. First, let's create a normal user.

groupadd tomcat #Add groupuseradd -g tomcat -s /usr/sbin/nologin tomcat #Add user to groupusermod -L tomcat #Lock the password and make it invalidpasswd tomcat #Set password

Through these four steps, we have created a common user. When creating a user, we first create a group. After the group is created, we create the user and join the group.

After the user is created, start setting permissions for the user

chown -R tomcat:tomcat /data #Assign permissions to users

This sets permissions for the tomcat user to operate the data directory and its subdirectories. -R represents the directory and its cascaded subdirectories.

[root@localhost data]# ls -l 
total 0 
drwxr-xr-x. 4 tomcat tomcat 79 May 20 08:03 tomcat 
[root@localhost data]#

At this point we can use the ls -l command to see that the data directory already belongs to the tomcat user and has view, write, and execute permissions

2. After completing the user creation, we will start to complete the tomcat startup script.

As shown in the code:

#!/bin/bash 
tomcat_home=/data/tomcat/tomcat-8484 
SHUTDOWN=$tomcat_home/bin/shutdown.sh 
STARTTOMCAT=$tomcat_home/bin/startup.sh 
case $1 in 
start) 
echo "Start $tomcat_home" 
$STARTTOMCAT 
cd /data/tomcat/tomcat-8484/logs 
tail -f catalina.out 
;; 
stop) 
echo "Close $tomcat_home" 
#$SHUTDOWN 
netstat -anp | grep 8484 | grep -v grep | awk '{print $7}' | sed -e 's//java//g' | sed -e 's/^/kill -9 /g' | sh 
#pidlist=`ps -ef |grep tomcat |grep -v "grep"|awk '{print $2}'` 
#kill -9 $pidlist 
#Delete the log file. If you don't delete it first, you can omit the following line #rm $tomcat_home/logs/* -rf 
#Delete the temporary directory of tomcat#rm $tomcat_home/work/* -rf 
;; 
restart) 
echo "Close $tomcat_home" 
$SHUTDOWN 
#pidlist=`ps -ef |grep tomcat |grep -v "grep"|awk '{print $2}'` 
#pidlist=`netstat -anp |grep 8484 |grep -v "grep"|awk '{print $2}'` 
#netstat -anp | grep 8484| grep -v grep | awk '{print $7}' | sed -e 's//java//g' | sed -e 's/^/kill -9 /g' | sh 
#kill -9 $pidlist 
sleep 5 
echo "Start $tomcat_home" 
$STARTTOMCAT 
#Look at the startup log#tail -f $tomcat_home/logs/catalina.out 
;; 
logs) 
cd /data/tomcat/tomcat-8484/logs 
tail -f catalina.out 
;; 
esac

The above code is the tomcat startup script. First, we need to create a text file and then change the suffix to .sh. Here I use tomcat with port number 8484 as an example. From the script file, you can see that you only need to customize your tomcat location and log location before you can use it. There are four commands in total: start, stop, restart, and logs.

After the script file is created, just put it in your server. You can choose any location. I put it in the bin directory under tomcat.

After the script file is placed on the server, it cannot be used yet. There are two problems to be solved, one is the format problem and the other is the permission problem. Because we created a text file, its format is text format, we want to change it to Unix format, so we need to set it as follows

sed -i "s/ 
//" tomcat-8484.sh #Set the script file to Unix format

After completing the format settings, you need to set permissions for the script file, because the default file permissions in Linux are drwxr-xr-x, that is, all run permissions are granted to the file owner, that is, the system administrator (I am currently logged in as the system administrator), and read and run permissions are granted to group users, and read permissions are granted to other users, so we need to reset the script file permissions.

chmod 777 ./tomcat-8484.sh

The chmod command is a command to change permissions. What does 777 mean?

In Linux systems, file or directory permissions are divided into three types: read-only, write-only, and executable.


According to the table above, the permission combination is the sum of the corresponding permission values, as follows:

7 = 4 + 2 + 1 Read, write and execute permissions

5 = 4 + 1 Read and execute permissions

4 = 4 read-only permission

Therefore, everyone understands the meaning of the chmod 777 ./tomcat-8484.sh command.

At this point our tomcat startup script is complete, let's demonstrate it below.

start up

[root@localhost bin]# ./tomcat-8484.sh start 
Start /data/tomcat/tomcat-8484 
Using CATALINA_BASE: /data/tomcat/tomcat-8484 
Using CATALINA_HOME: /data/tomcat/tomcat-8484 
Using CATALINA_TMPDIR: /data/tomcat/tomcat-8484/temp 
Using JRE_HOME: /usr 
Using CLASSPATH: /data/tomcat/tomcat-8484/bin/bootstrap.jar:/data/tomcat/tomcat-8484/bin/tomcat-juli.jar 
Tomcat started.

Let's check the process to see if it is actually started.

[root@localhost bin]# ps -ef |grep tomcat 
root 5569 1 7 14:09 pts/0 00:00:06 /usr/bin/java -Djava.util.logging.config.file=/data/tomcat/tomcat-8484/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dignore.endorsed.dirs= -classpath /data/tomcat/tomcat-8484/bin/bootstrap.jar:/data/tomcat/tomcat-8484/bin/tomcat-juli.jar -Dcatalina.base=/data/tomcat/tomcat-8484 -Dcatalina.home=/data/tomcat/tomcat-8484 -Djava.io.tmpdir=/data/tomcat/tomcat-8484/temp org.apache.catalina.startup.Bootstrap start 
root 5611 5340 0 14:10 pts/0 00:00:00 grep --color=auto tomcat

You can see that there is no problem with startup.

closure

[root@localhost bin]# ./tomcat-8484.sh stop 
Close /data/tomcat/tomcat-8484 
sh: line 2: kill: (18484) - No such process 
[root@localhost bin]# ps -ef |grep tomcat 
root 5621 5340 0 14:13 pts/0 00:00:00 grep --color=auto tomcat

Check that the process is indeed closed successfully, and our tomcat startup script is complete. This script can also be extended on this basis, such as checking the log immediately after startup.

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • How to start and stop Tomcat service by writing shell script in Linux environment
  • Windows uses batch processing to publish web to tomcat and start tomcat script sharing
  • Parsing Tomcat's startup script--startup.bat
  • Parsing Tomcat's startup script--catalina.bat
  • How to use batch scripts to determine the port and start Tomcat in Windows server
  • CentOS 6 configures tomcat8 to start the script

<<:  mysql query data for today, this week, this month, and last month

>>:  Summary of the execution issues between mysql max and where

Recommend

Linux's fastest text search tool ripgrep (the best alternative to grep)

Preface Speaking of text search tools, everyone m...

About the role of meta in HTML (collected and sorted from the Internet)

W3Cschool explains it this way The <meta> el...

Introduction to JavaScript Number and Math Objects

Table of contents 1. Number in JavaScript 2. Math...

Summary of commonly used multi-table modification statements in Mysql and Oracle

I saw this question in the SQL training question ...

Detailed explanation of CSS3 elastic expansion box

use Flexible boxes play a vital role in front-end...

Detailed explanation of how to use the Vue license plate search component

A simple license plate input component (vue) for ...

JavaScript Reflection Learning Tips

Table of contents 1. Introduction 2. Interface 3....

Let's deeply understand the event object in js

We know that the commonly used events in JS are: ...

DOM operation implementation in react

Table of contents Previous words Usage scenarios ...

Detailed explanation of selinux basic configuration tutorial in Linux

selinux ( Security-Enhanced Linux) is a Linux ker...

A brief introduction to Linux performance monitoring commands free

When the system encounters various IO bottlenecks...