添加红色这一行,才会启动tomcat生成pid文件
[root@tomcat01 work]# cat -n /aliyun/tomcat7/bin/catalina.sh |sed -n '128,132p'
128 [ -z "$CATALINA_BASE" ] && CATALINA_BASE="$CATALINA_HOME"
129
130 ###tomcat qidong jiaoben use by wujianwei 2016-5-11
131 [ -n "$CATALINA_HOME" ] && CATALINA_PID=$CATALINA_HOME/work/catalina.pid
132 # Ensure that any user defined CLASSPATH variables are not used on startup,
但是此参数生成的pid文件catalina.pid 里面记录的是 tomcat 日志切割进程的进程号并不是tomcat 服务的进程号。如果当时没有做日志切割,catalina.pid文件记录的是tomcat的进程号.(据猜测,日志切割和pid文件的生成和在/aliyun/tomcat7/bin/catalina.sh文件的设置参数的先后顺序有关。但是有待验证)
而且每次启动一次tomcat,/aliyun/tomcat7/bin/startup.sh 。tomcat的日志就会切割一个,新生成一个tomcat01.2016-05-12.out ,把之前的tomcat01.2016-05-12.out里面的日志内容全部覆盖掉了。(日志切割时按天进行切割的,所以当天再次启动tomcat生成的新的日志,日期还是当天的日期)
而且当tomcat 启动后再次执行/aliyun/tomcat7/bin/startup.sh 时,会有如下提示:
Using CATALINA_BASE: /aliyun/tomcat7
Using CATALINA_HOME: /aliyun/tomcat7
Using CATALINA_TMPDIR: /aliyun/tomcat7/temp
Using JRE_HOME: /aliyun/java-1.7.0
Using CLASSPATH: /aliyun/tomcat7/bin/bootstrap.jar:/aliyun/tomcat7/bin/tomcat-juli.jar
Using CATALINA_PID: /aliyun/tomcat7/work/catalina.pid
Existing PID file found during start.
Tomcat appears to still be running with PID 9610. Start aborted.
If the following process is not a Tomcat process, remove the PID file and try again:
UID PID PPID C STIME TTY TIME CMD
root 9610 1 0 11:54 pts/2 00:00:00 /usr/local/sbin/cronolog /aliyun/tomcat7/logs/tomcat01.2016-05-12.out

[root@tomcat01 tomcat7]# cat /aliyun/tomcat7/work/catalina.pid
9416
[root@tomcat01 tomcat7]# ps -ef|grep "/usr/local/sbin/cronolog"|grep -v grep
root 9416 1 0 11:35 pts/2 00:00:00 /usr/local/sbin/cronolog /aliyun/tomcat7/logs/tomcat01.2016-05-12.out

注意:虽然有上述的提示,但是不影响以下脚本的正常使用。
线上脚本文件:
[root@tomcat01 work]# cat /etc/init.d/tomcat7
#!/bin/bash
#chkconfig 2345 10 90
#description: start and stop tomcat service scripts
#author : wujianwei
#email : [email protected]
pid_file=/aliyun/tomcat7/work/catalina.pid
PID=$(ps -ef|grep "/aliyun/java-1.7.0/bin/java"|grep -v "grep"|awk '{print $2}'|xargs|awk '{print $1}'|grep -v "^$")
pid=$(ps -ef|grep "/aliyun/java-1.7.0/bin/java"|grep -v "grep"|awk '{print $2}'|xargs|awk '{print $1}'|grep -v "^$"|wc -l)
source /etc/profile
. /etc/init.d/functions
start() {
if [ -f $pid_file ]; then
action "tomcat7 service has start already!" /bin/false
else
/aliyun/tomcat7/bin/startup.sh >/dev/null
sleep 2
action "tomcat7 service is starting!" /bin/true
fi
}
stop() {
if [ -f $pid_file ]; then
rm -f $pid_file;
kill $PID;
sleep 1
action "tomcat7 service is stopped!" /bin/true
else
action "tomcat service has stop already" /bin/false
fi
}
status() {
if [ $pid -ne 0 ];then
echo "tomcat7 is running, pid is $PID!"
else
rm -f $pid_file
echo "tomcat7 has stoped!"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
stop;
sleep 1;
start
;;
*)
echo $"USEAGE: $0 {start|stop|status|restart}";;
esac
chmod +x /etc/init.d/tomcat7

[root@tomcat01 work]# chkconfig --add tomcat7
service tomcat7 does not support chkconfig