如何在系统重启时自动重启Tomcat7?

我在Ubuntu 12.04 LTS上安装了Tomcat 7,它运行在Amzon EC2实例上。 现在我希望tomcat在系统重启时自动重启。

我读了这个博客 ,建议将以下脚本添加到/etc/init.d/tomcat7

 # Tomcat auto-start # # description: Auto-starts tomcat # processname: tomcat # pidfile: /var/run/tomcat.pid case $1 in start) sh /usr/share/tomcat7/bin/startup.sh ;; stop) sh /usr/share/tomcat7/bin/shutdown.sh ;; restart) sh /usr/share/tomcat7/bin/shutdown.sh sh /usr/share/tomcat7/bin/startup.sh ;; esac exit 0 

并发出以下命令:

 sudo chmod 755 /etc/init.d/tomcat7 sudo ln -s /etc/init.d/tomcat7 /etc/rc1.d/K99tomcat sudo ln -s /etc/init.d/tomcat7 /etc/rc2.d/S99tomcat sudo /etc/init.d/tomcat7 restart 

我的问题

  1. tomcat7已经有了脚本,我们在哪里粘贴建议的脚本?
  2. 建议的程序是否正确?

在/etc/init.d/tomcat7中创建init脚本,其内容如下(你的脚本应该也可以,但我认为这个标准更贴近标准)。

这样Tomcat只有在配置了网络接口后才会启动。

初始脚本内容:

 #!/bin/bash ### BEGIN INIT INFO # Provides: tomcat7 # Required-Start: $network # Required-Stop: $network # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Start/Stop Tomcat server ### END INIT INFO PATH=/sbin:/bin:/usr/sbin:/usr/bin start() { sh /usr/share/tomcat7/bin/startup.sh } stop() { sh /usr/share/tomcat7/bin/shutdown.sh } case $1 in start|stop) $1;; restart) stop; start;; *) echo "Run as $0 "; exit 1;; esac 

更改其权限并自动添加正确的符号链接:

 chmod 755 /etc/init.d/tomcat7 update-rc.d tomcat7 defaults 

从现在开始,它将在进入适当的运行级别时自动启动和关闭。 您也可以使用service tomcat7 来控制它

 #!/bin/bash # # Author : subz # Copyright (c) 2k15 # # Make kill the tomcat process # TOMCAT_HOME=/media/subin/works/Applications/apache-tomcat-7.0.57 SHUTDOWN_WAIT=5 tomcat_pid() { echo `ps aux | grep org.apache.catalina.startup.Bootstrap | grep -v grep | awk '{ print $2 }'` } start() { pid=$(tomcat_pid) if [ -n "$pid" ] then echo "Tomcat is already running (pid: $pid)" else # Start tomcat echo "Starting tomcat" /bin/sh $TOMCAT_HOME/bin/startup.sh fi return 0 } stop() { pid=$(tomcat_pid) if [ -n "$pid" ] then echo "Stoping Tomcat" /bin/sh $TOMCAT_HOME/bin/shutdown.sh let kwait=$SHUTDOWN_WAIT count=0; until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ] do echo -n -e "\nwaiting for processes to exit"; sleep 1 let count=$count+1; done if [ $count -gt $kwait ]; then echo -n -e "\nkilling processes which didn't stop after $SHUTDOWN_WAIT seconds" kill -9 $pid echo " \nprocess killed manually" fi else echo "Tomcat is not running" fi return 0 } pid=$(tomcat_pid) if [ -n "$pid" ] then echo "Tomcat is running with pid: $pid" stop else echo "Tomcat is not running" start fi exit 0 

不能将它添加到/etc/rc.local

 #!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. sleep 10 /usr/share/tomcat7/bin/startup.sh 

Digital Ocean为使用Tomcat 8.x和Ubuntu 16.04 LTS和systemd脚本提供了非常方便的指南。

https://www.digitalocean.com/community/tutorials/how-to-install-apache-tomcat-8-on-ubuntu-16-04

apache tomcat不会随附任何init脚本。

  1. 从Ubuntu软件包管理器安装由Ubuntu维护的预打包版本,此版本附带自己的init脚本。

  2. 按照您提到的博客中的步骤为您提供kickstart init脚本。