Centos下实现service tomcat start|stop|restart,启动、停止、重启tomcat服务

转载于:http://mangocool.com/detail_1_1444464567032.html

昨天写的文章《Centos下shell脚本的定时任务》中,提到使用service tomcat start启动tomcat服务的命令。有小伙伴就问如何实现,我知道通常情况下都是使用比较原始的方式,进入tomcat的bin目录后执行对应shell脚本。如果长此以往,你会很累的,因为每一次操作会多消耗0.06卡,相当于你走了两步。成功程序员要素的第一条就是偷懒,所以你还不来试试吗!

直接贴shell脚本代码了,亲自验证过了,可以用:
#!/bin/bash

chkconfig: 2345 10 90

description: Starts and Stops the Tomcat daemon.

TOMCAT_HOME=/home/software/tomcat7 #tomcat dir
TOMCAT_START= T O M C A T H O M E / b i n / s t a r t u p . s h T O M C A T S T O P = TOMCAT_HOME/bin/startup.sh TOMCAT_STOP= TOMCATHOME/bin/startup.shTOMCATSTOP=TOMCAT_HOME/bin/shutdown.sh

necessary environment variables export

CATALINA_HOME=$TOMCAT_HOME
export JAVA_HOME=/usr/local/jdk1.7.0_80 #jdk dir

source function library.

. /etc/rc.d/init.d/functions

check that networking is up.

[ “${NETWORKING}” = “no” ] && exit 0

check for tomcat script

if [ ! -f $TOMCAT_HOME/bin/catalina.sh ]; then
echo “Tomcat not valilable…”
exit
fi
start(){
echo -n "Starting Tomcat: "
daemon $TOMCAT_START
echo
touch /var/lock/subsys/tomcat
}
stop(){
echo -n $"Shutting down Tomcat: "
daemon $TOMCAT_STOP
rm -f /var/lock/subsys/tomcat.pid echo
}
restart(){
stop
start
}
status(){
ps ax --width=1000 | grep “[o]rg.apache.catalina.startup.Bootstrap start” | awk ‘{printf $1 " "}’
| wc | awk ‘{print $2}’ > /tmp/tomcat_process_count.txt
read line < /tmp/tomcat_process_count.txt
if [ $line -gt 0 ]; then
echo -n "tomcat ( pid "
ps ax --width=1000 | grep “org.apache.catalina.startup.Bootstrap start” | awk ‘{printf $1 " "}’
echo -n “) is running…”
echo
else
echo “Tomcat is stopped”
fi
}
case “$1” in
start)
start ;;
stop)
stop ;;
restart)
stop
sleep 3
start ;;
status)
status ;;
*)
echo “Usage: tomcatd {start|stop|restart|status}”
exit 1
esac
exit 0

chkconfig: 2345 10 90

description: Starts and Stops the Tomcat daemon.

TOMCAT_HOME=/home/software/tomcat7 #tomcat dir
TOMCAT_START= T O M C A T H O M E / b i n / s t a r t u p . s h T O M C A T S T O P = TOMCAT_HOME/bin/startup.sh TOMCAT_STOP= TOMCATHOME/bin/startup.shTOMCATSTOP=TOMCAT_HOME/bin/shutdown.sh

necessary environment variables export

CATALINA_HOME=$TOMCAT_HOME
export JAVA_HOME=/usr/local/jdk1.7.0_80 #jdk dir

source function library.

. /etc/rc.d/init.d/functions

check that networking is up.

[ “${NETWORKING}” = “no” ] && exit 0

check for tomcat script

if [ ! -f $TOMCAT_HOME/bin/catalina.sh ]; then
echo “Tomcat not valilable…”
exit
fi
start(){
echo -n "Starting Tomcat: "
daemon $TOMCAT_START
echo
touch /var/lock/subsys/tomcat
}
stop(){
echo -n $"Shutting down Tomcat: "
daemon $TOMCAT_STOP
rm -f /var/lock/subsys/tomcat.pid echo
}
restart(){
stop
start
}
status(){
ps ax --width=1000 | grep “[o]rg.apache.catalina.startup.Bootstrap start” | awk ‘{printf $1 " "}’
| wc | awk ‘{print $2}’ > /tmp/tomcat_process_count.txt
read line < /tmp/tomcat_process_count.txt
if [ $line -gt 0 ]; then
echo -n "tomcat ( pid "
ps ax --width=1000 | grep “org.apache.catalina.startup.Bootstrap start” | awk ‘{printf $1 " "}’
echo -n “) is running…”
echo
else
echo “Tomcat is stopped”
fi
}
case “$1” in
start)
start ;;
stop)
stop ;;
restart)
stop
sleep 3
start ;;
status)
status ;;
*)
echo “Usage: tomcatd {start|stop|restart|status}”
exit 1
esac
exit 0
进入/etc/init.d目录,执行命令:

?
1
vim tomcat
将上述代码贴进去,保存并退出。

最后别忘了,更改脚本权限:

?
1
chmod 755 tomcat

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值