Tomcat非root身份运行制作Linux系统服务管理

理论知识怱略,马上开始实战

一、首先准备好tomcat 启动、关闭、重启Shell脚本:

以下Shell脚本主要修改值

tomcatPath:tomcat目录

runUser:以哪个身份运行

此处测试的Shell脚本存放地址如下:

mkdir /usr/local/apache-tomcat-7.0.94/sbin/
vi /usr/local/apache-tomcat-7.0.94/sbin/tomcat-server
#复制tomcat-server的代码

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

二、创建运行用户,这里以用户名:tomcat 为例

useradd tomcat

三、把tomcat目录权授

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

注意:

  程序用到的所有目录都需要授权给该用户,否则会报权限不足,导致执行失败,执行命令同上

四、开始编写systemctl启动的配置文件

vi /lib/systemd/system/tomcat.service

[Unit]
Description=Tomcat server daemon
Documentation=/data/application/apache-tomcat-8.5.38
#注意:一般tomcat服务都是在数据库或redis等启动之后,再运行,确保数据库或redis有制作systemctl,在配置到After,mysql.service redisd.service名字是编写systemctl时的文件名
After=network.target mysql.service redisd.service

[Service]
Type=forking
PIDFile=/usr/local/apache-tomcat-7.0.94/tomcat.pid
ExecStart=/usr/local/apache-tomcat-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、配置Tomcat Pid生产的位置

注意:

  确认Tomcat PID必须 与 /lib/systemd/system/tomcat.service 配置里面的 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、刷新systemctl配置

#重新加载配置文件
systemctl daemon-reload

#服务开启,关闭,重启
systemctl start tomcat.service
systemctl stop tomcat.service
systemctl restart tomcat.service

#设置开机自动启动
systemctl enable tomcat.service

注意:

  启动完成后,需要检查进程是否运行

ps -ef | grep java

正常启动如下:

 

你可能感兴趣的:(Tomcat非root身份运行制作Linux系统服务管理)