简单使用普通用户启动tomcat
新建用户tomcat,该用户不能登录
useradd tomcat -s '/sbin/nologin'
将/usr/local/tomcat/bin/startup.sh更名
mv /usr/local/tomcat/bin/startup.sh /usr/local/tomcat/bin/startup_.sh
将/usr/local/tomcat/bin/shutdown.sh更名
mv /usr/local/tomcat/bin/shutdown.sh /usr/local/tomcat/bin/shutdown_.sh
新建启动脚本startup.sh
touch /usr/local/tomcat/bin/startup.sh
其内容如下:
#!/bin/bash ## set user tomcat to /bin/bash to start server usermod -s /bin/bash tomcat ## use tomcat user to start server su - tomcat -c /usr/local/tomcat_web/bin/startup_.sh ## set user tomcat to /sbin/nologin usermod -s /sbin/nologin tomcat
新建关闭脚本shutdown.sh
touch /usr/local/tomcat/bin/shutdown.sh
内容如下:
#!/bin/bash ## set user tomcat to /bin/bash to shutdown server usermod -s /bin/bash tomcat ## use tomcat user to shudown server su - tomcat -c /usr/local/tomcat_web/bin/shuwdown_.sh ## set user tomcat to /sbin/nologin usermod -s /sbin/nologin tomcat
设置tomcat用户权限
chown -R tomcat:tomcat /usr/local/tomcat/ chmod -R 744 /usr/local/tomcat
判断tomcat用户是否存在
ret=`id -u tomcat >& /dev/null` if [ $? -ne 0 ];then echo "not exist" fi
也可以普通用户使用开机启动服务启动tomcat
/usr/lib/systemd/system/下新建文件tomcat.service,将下面内容拷贝其中
[Unit]
Description=Tomcat
#After=syslog.target network.target remote-fs.target nss-lookup.target
After=syslog.target network.target remote-fs.target nss-lookup.target redis.service mysql.service
Requires=mysql.service redis.service
[Service]
Type=forking
PIDFile=/usr/local/tomcat/tomcat.pid
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/tomcat/bin/shutdown.sh
PrivateTmp=true
User=tomcat
[Install]
WantedBy=multi-user.target
注册到系统服务
systemctl enable tomcat.service
使新注册服务生效
systemctl daemon-reload