Ubuntu下设置Tomcat开机自动启动

3.1 在/etc/init.d目录下新建tomcat文件

sudo cd /etc/init.d
sudo vi tomcat
3.2 添加启动、关闭、重启的shell脚本

#!/bin/sh

BEGIN INIT INFO

Provides: tomcat

Required-Start: $remote_fs $network

Required-Stop: $remote_fs $network

Default-Start: 2 3 4 5

Default-Stop: 0 1 6

Short Description: the tomcat Java Application Server

END INIT INFO

RETVAL=0
export JRE_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export CATALINA_HOME=/home/xxx/apache-tomcat-9.0.11 # xxx为自己的用户名
export CATALINA_BASE=/home/xxx/apache-tomcat-9.0.11 # xxx为自己的用户名

start()
{
if [ -f $CATALINA_HOME/bin/startup.sh ]
then
echo $”Starting Tomcat”
$CATALINA_HOME/bin/startup.sh
RETVAL=$?
echo “OK”
return $RETVAL
fi
}

stop()
{
if [ -f $CATALINA_HOME/bin/shutdown.sh ]
then
echo $”Stopping Tomcat”
$CATALINA_HOME/bin/shutdown.sh
RETVAL=$?
sleep 3
ps -fwwu tomcat | grep apache-tomcat | grep -v grep | grep -v PID | awk ‘{print $2}’| xargs kill -9
echo “OK”
return $RETVAL
fi
}

case “$1” in
start)
start
;;
stop)
stop
;;
restart)
echo $”Restarting Tomcat”
$0 stop
sleep 3
$0 start
;;
*)
echo $”Usage:$0{start|stop|restart}”
exit 1
;;
esac

exit $RETVAL
3.3 保存退出,授予tomcat文件执行权限

chmod +x tomcat
3.4 使tomcat服务生效,并使能开机自启动,然后reboot重启,即可Tomcat开机自启动生效.

sudo systemctl daemon-reload #对于新创建的unit文件或修改了的unit文件,要通知systemd重载此配置文件;
sudo systemctl enable tomcat #使能开机自启动
sudo reboot
3.5 重启后,执行systemctl status tomcat看看tomcat服务的状态。

root@localhost:~# systemctl status tomcat
● tomcat.service
Loaded: loaded (/etc/init.d/tomcat; bad; vendor preset: enabled)
Active: active (running) since Thu 2018-08-23 21:54:25 PDT; 44s ago
Docs: man:systemd-sysv-generator(8)
Process: 811 ExecStart=/etc/init.d/tomcat start (code=exited, status=0/SUCCESS)
CGroup: /system.slice/tomcat.service
└─832 /usr/lib/jvm/java-8-openjdk-amd64/bin/java -Djava.util.logging.config.file=/home/localhost/apache-tomcat-9.0.11/conf/logging.propertie

Aug 23 21:54:24 localhost systemd[1]: Starting tomcat.service…
Aug 23 21:54:25 localhost tomcat[811]: $Starting Tomcat
Aug 23 21:54:25 localhost tomcat[811]: Tomcat started.
Aug 23 21:54:25 localhost tomcat[811]: OK
Aug 23 21:54:25 localhost systemd[1]: Started tomcat.service.


作者:相信美好的事情将会发生
来源:CSDN
原文:https://blog.csdn.net/bbaaEE/article/details/82015155
版权声明:本文为博主原创文章,转载请附上博文链接!

-------------本文结束感谢您的阅读-------------