shell文件:
#!/bin/sh #关闭进程 killpids(){ pids=($(ps -ef | grep $1 | awk '{print $2}')) for pid in ${pids[@]}; do echo "关闭进程: pid ${pid}" kill -9 ${pid} done } killpids tomcat #搜索可能的安装目录 echo "Directory list:" paths=($(find / -maxdepth 5 -type d -name '*tomcat*')) if [ ${#paths[@]} -lt 1 ];then echo "Could not find your tomcat directory!" exit fi for((i=0; i<${#paths[@]}; i++));do if [[ ! ${paths[i]} =~ "tmp" ]] then echo "$i. ${paths[i]}" fi done echo -n "Which one is the root directory of tomcat? " #读取根目录 while(true) do read sn if [[ $sn -ge 0 && $sn -lt ${#paths[@]} ]] then path=${paths[sn]} echo $path break else echo "$sn is not expected" fi done #设置环境变量 sed -i '/CATALINA_HOME/d' /etc/profile echo "设置环境变量" echo "export CATALINA_HOME=$path" >> /etc/profile source /etc/profile #开机启动 #1.添加 /etc/rc.d/init.d/tomcat rm -rf /etc/rc.d/init.d/tomcat touch /etc/rc.d/init.d/tomcat chmod +x /etc/rc.d/init.d/tomcat echo '#!/bin/bash # chkconfig: 2345 89 2 # Description: Tomcat Server basic start/shutdown script # /etc/init.d/tomcat -- startup script for the Tomcat servlet engine ' >> /etc/rc.d/init.d/tomcat echo "TOMCAT_HOME=$path/bin START_TOMCAT=$path/bin/startup.sh STOP_TOMCAT=$path/bin/shutdown.sh " >> /etc/rc.d/init.d/tomcat echo 'start() { echo -n "Starting tomcat: " cd $TOMCAT_HOME ${START_TOMCAT} echo "done." } stop() { echo -n "Shutting down tomcat: " cd $TOMCAT_HOME ${STOP_TOMCAT} echo "done." } case "$1" in start) start ;; stop) stop ;; restart) stop sleep 10 start ;; *) echo "Usage: $0 {start|stop|restart}" esac exit 0' >> /etc/rc.d/init.d/tomcat #2.添加到chkconfig chkconfig --add tomcat chkconfig tomcat on service tomcat restart运行即可,中间要选择 tomcat 目录