Tomcat running as non-root production Linux systems management services

Theoretical knowledge a little hasty, immediately began combat

First, the first ready tomcat startup, shutdown, restart Shell script:

The following Shell scripts major modification value

tomcatPath: tomcat directory

runUser: Run to which identity

Shell scripts stored address tested here as follows:

mkdir / usr / local / tomcat--Apache 7.0 . 94 / sbin / 
VI / usr / local / tomcat--Apache 7.0 . 94 / sbin / tomcat-server
# copy tomcat-server code

chmod + x / usr / local / apache-tomcat-7.0.94 / sbin /  tomcat-server

 

#!/bin/bash
#Authtor : suk
#Date : 20180428
#Email : 277667028@qq.com

. /etc/init.d/functions

#Result Code
RETVAL=0

#Tomcat Pid
TomcatPid=null

#Tomcat Home
tomcatPath=/usr/local/application/apache-tomcat-7.0.65

runUser='tomcat'


getTomcatPid()
{
        TomcatPid=$(ps -ef | grep java |grep tomcat | grep -w $tomcatPath | grep -v 'grep ' | awk '{print $2}')


}

checkTocamtPid()
{
        getTomcatPid
        if [ -n "$TomcatPid" ];
        then
            #run
            RETVAL=1
        else
            #no run
            RETVAL=0
        fi
}

start()
{
        checkTocamtPid
        if [ $RETVAL -eq 1 ];
            then
                getTomcatPid
                action "Tomcat Already Running Pid:【$TomcatPid】" /bin/false
                exit $RETVAL
        else
                /bin/sudo su - $runUser $tomcatPath/bin/startup.sh
                getTomcatPid
                action "Start Tomcat Success Pid:【$TomcatPid】" /bin/true
        fi
}

restart()
{
        checkTocamtPid
        if [ $RETVAL -eq 1 ];
            then
                stop
                sleep 1
        fi
        start
}

stop()
{
        checkTocamtPid


        if [ $RETVAL -eq 1 ];
            then
                getTomcatPid
                /bin/kill -9 $TomcatPid
                action "Stop Tomcat PID:【$TomcatPid】" /bin/true
        else
                action "Tomcat Status No Running" /bin/false
        fi
}




#Check Tomcat Status
status()
{
        checkTocamtPid
        [ $RETVAL -eq 1  ] && echo "Tomcat is Running Pid:【$TomcatPid】" || echo "No Tomcat Running"
}




#Check Tomcat Start Logs
logs(){
        logsPath=$tomcatPath/logs/catalina.out
        if [ ! -f $logsPath ];then
            echo "$logsPath Not Eixts"
        else
            /usr/bin/tail -f  $logsPath
            exit $RETVAL
        fi
}

case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        restart)
                restart
                ;;
        status)
                status
                ;;
                logs)
                logs
                ;;
        *)
                echo $"Usage: $0 {start|stop|restart|status|logs}"
esac
tomcat-server

Second, create a user operation, the user name here: tomcat, for example

useradd tomcat

Third, the right to grant tomcat directory

chown -R tomcat.tomcat /usr/local/apache-tomcat-7.0.94

note:

  All directories used by the program need to be licensed to the user, otherwise it will be reported insufficient privileges, resulting in a failure to perform, execute the command as above

Fourth, start writing systemctl initiated profile

VI / lib / systemd / System / tomcat.service 

[Unit] 
the Description = the Tomcat Server daemon 
Documentation = / Data / file application / tomcat--Apache 8.5 . 38 is 
# Note: Usually tomcat services are redis database or the like after the start, and then run, ensure that the database or redis have made systemctl, in the configuration After, mysql.service redisd.service name is the file name when writing systemctl 
the After = network.target mysql.service redisd.service 

[Service] 
Type = forking 
the PidFile = / usr / local / tomcat--Apache 7.0 . 94 / tomcat.pid 
ExecStart = / usr / local / tomcat--Apache 7.0 . 94 / sbin / tomcat-server start
ExecStop=/usr/local/apache-tomcat-7.0.94/sbin/tomcat-server stop
Restart=/usr/local/apache-tomcat-7.0.94/sbin/tomcat-server restart
PrivateTmp=True

[Install]
WantedBy=multi-user.target

 

5, the arrangement position Tomcat Pid production

note:

  Confirm Tomcat PID must /lib/systemd/system/tomcat.service consistent configuration inside PIDFile

vi /usr/local/apache-tomcat-7.0.94/bin/catalina.sh
怱略
#
#   USE_NOHUP       (Optional) If set to the string true the start command will
#                   use nohup so that the Tomcat process will ignore any hangup
#                   signals. Default is "false" unless running on HP-UX in which
#                   case the default is "true"
# -----------------------------------------------------------------------------

CATALINA_PID=/usr/local/apache-tomcat-7.0.94/tomcat.pid

# OS specific support.  $var _must_ be set to either true or false.
cygwin=false
怱略

6, refresh the configuration systemctl

# Reload the configuration file 
systemctl daemon - reload 

# services on, off, restart 
systemctl Start tomcat.service 
systemctl STOP tomcat.service 
systemctl restart tomcat.service 

# settings at startup 
systemctl enable tomcat.service

note:

  After startup, you need to check whether the process is running

ps -ef | grep java

Normal start follows:

 

Guess you like

Origin www.cnblogs.com/ygbh/p/11956598.html