Tomcat启动脚本
跳到导航
跳到搜索
sysv
#!/bin/bash #cat /etc/init.d/pp-col 在centos7上试过 可运行 在测试的机器上已跑了 有空看一下sysd的怎么搞 # chkconfig: 345 99 28 # description: Starts/Stops Apache Tomcat #by evan #修改几个目录 if new ##export JAVA_HOME=/usr/java/jdk1.8.0_121/ #sed -i "s/JAVA_HOME=\/usr\/java\/default\//JAVA_HOME=\/usr\/java\/jdk1.8.0_121\//g" pp-collector.init #sed -i "s/CATALINA_HOME=\/data\/service\/pinpoint-collector\//CATALINA_HOME=\/data\/pp-col\//g" pp-collector.init #sed -i "s/CATALINA_BASE=\/data\/service\/pinpoint-collector\//CATALINA_BASE=\/data\/pp-col\//g" pp-collector.init #if old #sed -i "s/JAVA_HOME=\/usr\/java\/default\//JAVA_HOME=\/usr\/java\/jdk1.7.0_75\//g" /etc/init.d/tomcat3 #sed -i "s/CATALINA_HOME=\/data\/pp-col\//CATALINA_HOME=\/home\/appserver\/tomcat3\//g" /etc/init.d/tomcat3 #sed -i "s/CATALINA_BASE=\/data\/pp-col\//CATALINA_BASE=\/home\/appserver\/tomcat3\//g" /etc/init.d/tomcat3 #by evan # Tomcat start/stop/status script # Forked from: https://gist.github.com/valotas/1000094 # @author: Miglen Evlogiev <[email protected]> # Release updates: # Updated method for gathering pid of the current proccess # Added usage of CATALINA_BASE # Added coloring and additional status # Added check for existence of the tomcat user #Location of JAVA_HOME (bin files) export JAVA_HOME=/usr/java/jdk1.8.0_121/ #Add Java binary files to PATH export PATH=$JAVA_HOME/bin:$PATH #CATALINA_HOME is the location of the bin files of Tomcat export CATALINA_HOME=/data/pp-col/ #CATALINA_BASE is the location of the configuration files of this instance of Tomcat export CATALINA_BASE=/data/pp-col/ #TOMCAT_USER is the default user of tomcat export TOMCAT_USER=root #TOMCAT_USAGE is the message if this script is called without any options TOMCAT_USAGE="Usage: $0 {\e[00;32mstart\e[00m|\e[00;31mstop\e[00m|\e[00;32mstatus\e[00m|\e[00;31mrestart\e[00m}" #SHUTDOWN_WAIT is wait time in seconds for java proccess to stop SHUTDOWN_WAIT=3 tomcat_pid() { echo `ps -fe | grep $CATALINA_BASE | grep -v grep | tr -s " "|cut -d" " -f2` } start() { pid=$(tomcat_pid) if [ -n "$pid" ] then echo -e "\e[00;31mTomcat is already running (pid: $pid)\e[00m" else # Start tomcat echo -e "\e[00;32mStarting tomcat\e[00m" #ulimit -n 100000 #umask 007 #/bin/su -p -s /bin/sh tomcat if [ `user_exists $TOMCAT_USER` = "1" ] then su $TOMCAT_USER -c $CATALINA_HOME/bin/startup.sh else sh $CATALINA_HOME/bin/startup.sh fi status fi return 0 } status(){ pid=$(tomcat_pid) if [ -n "$pid" ]; then echo -e "\e[00;32mTomcat is running with pid: $pid\e[00m" else echo -e "\e[00;31mTomcat is not running\e[00m" fi } stop() { pid=$(tomcat_pid) if [ -n "$pid" ] then echo -e "\e[00;31mStoping Tomcat\e[00m" #/bin/su -p -s /bin/sh tomcat sh $CATALINA_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 "\n\e[00;31mwaiting for processes to exit\e[00m"; sleep 1 let count=$count+1; done if [ $count -gt $kwait ]; then echo -n -e "\n\e[00;31mkilling processes which didn't stop after $SHUTDOWN_WAIT seconds\e[00m" kill -9 $pid fi else echo -e "\e[00;31mTomcat is not running\e[00m" fi return 0 } user_exists(){ if id -u $1 >/dev/null 2>&1; then echo "1" else echo "0" fi } case $1 in start) start ;; stop) stop ;; restart) stop start ;; status) status ;; *) echo -e $TOMCAT_USAGE ;; esac exit 0
sysd
#这个上次好像试了不太好 2.2 创建Tomcat启动参数setenv.sh文件 catalina.sh在执行的时候会调用同级路径下的setenv.sh来设置额外的环境变量,因此需要在/usr/local /tomcat/bin路径下创建setenv.sh文件。 执行脚本: [zm@zm-centos7 ~]$ su # 切换至root用户 密码: [root@zm-centos7 zm]# vim /usr/local/tomcat/bin/setenv.sh # 创建setenv.sh脚本 [root@zm-centos7 zm]# cat /usr/local/tomcat/bin/setenv.sh # 查看setenv.sh脚本内容 vi /home/appserver/tomcat3/bin/setenv.sh export CATALINA_HOME=/home/appserver/tomcat3/ export CATALINA_BASE=/home/appserver/tomcat3/ #设置Tomcat的PID文件 CATALINA_PID="$CATALINA_BASE/tomcat3.pid" #添加JVM选项 JAVA_OPTS="-server -XX:PermSize=256M -XX:MaxPermSize=1024m -Xms512M -Xmx1024M -XX:MaxNewSize=256m" chmod +x setenv.sh #bak export CATALINA_HOME=/usr/local/tomcat export CATALINA_BASE=/usr/local/tomcat #设置Tomcat的PID文件 CATALINA_PID="$CATALINA_BASE/tomcat.pid" #添加JVM选项 JAVA_OPTS="-server -XX:PermSize=256M -XX:MaxPermSize=1024m -Xms512M -Xmx1024M -XX:MaxNewSize=256m" 2.3 编写tomcat.service文件 在/usr/lib/systemd/system路径下添加tomcat.service文件。 注意:文件中目录路径必须是绝对路径 执行脚本: [root@zm-centos7 zm]# vim /usr/lib/systemd/system/tomcat3.service #创建tomcat.service服务脚本 [root@zm-centos7 zm]# cat /usr/lib/systemd/system/tomcat3.service #查看tomcat.service服务脚本 [Unit] Description=Tomcat3 After=syslog.target network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/home/appserver/tomcat3/tomcat3.pid ExecStart=/home/appserver/tomcat3/bin/startup.sh ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target [unit]配置了服务的描述,规定了在network启动之后执行, [service]配置服务的pid,服务的启动,停止,重启 [install]配置了使用用户 --------- #这个还没测试过 不过应该是可以 的 [Unit] Description=Tomcat 8.5 servlet container After=network.target [Service] Type=forking User=tomcat Group=tomcat Environment="JAVA_HOME=/usr/lib/jvm/default-java" Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom" Environment="CATALINA_BASE=/opt/tomcat/latest" Environment="CATALINA_HOME=/opt/tomcat/latest" Environment="CATALINA_PID=/opt/tomcat/latest/temp/tomcat.pid" Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC" ExecStart=/opt/tomcat/latest/bin/startup.sh ExecStop=/opt/tomcat/latest/bin/shutdown.sh [Install] WantedBy=multi-user.target 通知systemd我们创建了一个新的单元文件并通过执行以下命令来启动Tomcat服务: sudo systemctl daemon-reload sudo systemctl start tomcat 您可以使用以下命令检查服务状态: sudo systemctl status tomcat 2.4 测试Tomcat.service服务 执行脚本: [root@zm-centos7 zm]# systemctl enable tomcat3 # 设置开机自启动 Created symlink from /etc/systemd/system/multi-user.target.wants/tomcat.service to /usr/lib/systemd/system/tomcat.service. # 自动创建软连接 [root@zm-centos7 zm]# systemctl start tomcat3 # 启动tomcat服务 [root@zm-centos7 zm]# systemctl status tomcat3 # 查看tomcat服务状态 ● tomcat.service - Tomcat8 =使用tomcat.service= systemctl enable tomcat.service systemctl start tomcat.service systemctl stop tomcat.service systemctl restart tomcat.service =trouble= [root@oracle-zimg-etc ~]# /home/appserver/tomcat3/bin/shutdown.sh Using CATALINA_BASE: /home/appserver/tomcat3/ Using CATALINA_HOME: /home/appserver/tomcat3/ Using CATALINA_TMPDIR: /home/appserver/tomcat3//temp Using JRE_HOME: /usr/java/jdk1.7.0_75 Using CLASSPATH: /home/appserver/tomcat3//bin/bootstrap.jar:/home/appserver/tomcat3//bin/tomcat-juli.jar Using CATALINA_PID: /home/appserver/tomcat3//tomcat3.pid $CATALINA_PID was set but the specified file does not exist. Is Tomcat running? Stop aborted. 程序在的 但是停止时会这样 也就是 自带的停止脚本会无效 哦 第一次就会这样 第一次强制停止 以后的 以后的脚本 =see also= [https://blog.csdn.net/oaim_zh/article/details/78319863 CentOS7下利用systemd机制实现tomcat开机自启动]