标签:服务管理 java进程管理 平滑重启java进程 进程信号 启动控制java服务进程
运维经验分享作为一个专题,目前共7篇文章
====================================分割线======================================
针对上一篇文章《运维经验分享(四)--关于 java进程管理的服务控制脚本编程思路分析》
最初版本(由别人写的):
#!/bin/sh
#chkconfig: 345 86 14
#description: Startup and shutdown script for ChatterServer
VERSION=1.0.0-snapshot
PIDFILE=./pid/chatter.pid
SERVER=./chatter-$VERSION\.jar
ARGS="-Xms2g -Xmx2g -Xmn2g -Xss128k"
# -Xms2g -Xmx2g -Xmn2g -Xss128k -XX:MaxPermSize=64m -XX:-UseParallelGC -XX:+UseParallelOldGC -XX:ParallelGCThreads=4 -XX:+UseConcMarkSweepGC -XX:MaxTenuringThreshold=30 -XX:SurvivorRatio=6
start()
{
if test -e $SERVER
then
echo "Starting $SERVER"
if java -jar $SERVER $ARGS >./logs/console.out 2>&1 &
then
echo $! > $PIDFILE
echo "server start OK"
else
echo "server start failed"
fi
else
echo "Couldn‘t find $SERVER"
fi
}
stop()
{
if test -e $PIDFILE
then
echo "Stopping server"
if kill `cat $PIDFILE` 2>./logs/console.out
then
echo "server stop OK"
else
echo "server stop faild"
fi
else
echo "No server running"
fi
}
restart()
{
echo "Restarting server"
stop
start
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|list}" >&2
exit 1
;;
esac
exit 0第一版本(我写的):
#!/bin/bash
#chkconfig: 345 86 14
#description: Startup and shutdown script for ChatterServer
VERSION=1.0.0-snapshot
PIDFILE=./pid/chatter.pid
SERVER=./chatter-$VERSION\.jar
ARGS="-Xms2g -Xmx4g -Xmn4g -Xss128k -XX:MaxPermSize=64m -XX:-UseParallelGC -XX:+UseParallelOldGC -XX:ParallelGCThreads=4 -XX:+UseConcMarkSweepGC -XX:MaxTenuringThreshold=30 -XX:SurvivorRatio=6"
# -Xms2g -Xmx2g -Xmn2g -Xss128k -XX:MaxPermSize=64m -XX:-UseParallelGC -XX:+UseParallelOldGC -XX:ParallelGCThreads=4 -XX:+UseConcMarkSweepGC -XX:MaxTenuringThreshold=30 -XX:SurvivorRatio=6
start()
{
if test -f $PIDFILE
then
if test -d /proc/$(cat $PIDFILE)
then
echo "server has started"
exit 1
fi
fi
if test -e $SERVER
then
echo "Starting $SERVER"
if java -jar $SERVER $ARGS >>./logs/console.out 2>&1 &
then
sleep 2
if [[ -d /proc/$! ]]; then
echo $! > $PIDFILE
echo "server start OK"
fi
elif java -jar $SERVER $ARGS >>./logs/console.out 2>&1 &
then
sleep 2
if [[ -d /proc/$! ]]; then
echo $! > $PIDFILE
echo "server start OK"
fi
else
echo "server start failed"
exit 1
fi
else
echo "Couldn‘t find $SERVER"
exit 1
fi
}
stop()
{
if test -e $PIDFILE
then
echo "Stopping server"
if kill -TERM `cat $PIDFILE` >>./logs/console.out 2>&1
then
sleep 2
if test -d /proc/$(cat $PIDFILE)
then
echo "server has started"
sleep 60
fi
echo "server stop OK"
/bin/rm $PIDFILE
elif
kill -KILL `cat $PIDFILE` >>./logs/console.out 2>&1
then
sleep 2
if test -d /proc/$(cat $PIDFILE)
then
echo "server has started"
sleep 60
fi
echo "server stop OK"
/bin/rm $PIDFILE
elif
kill -9 `cat $PIDFILE` >>./logs/console.out 2>&1
then
sleep 2
if test -d /proc/$(cat $PIDFILE)
then
echo "server has started"
sleep 60
fi
echo "server stop OK"
/bin/rm $PIDFILE
else
echo "server stop failed"
exit 1
fi
else
echo "No server running"
exit 1
fi
}
restart()
{
echo "Restarting server"
stop
start
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo "Usage: $0 {start|stop|restart}" >&2
exit 1
;;
esac
exit 0第二版本(最后针对stop服务进程再次优化过的):
#!/bin/bash
#chkconfig: 345 86 14
#description: Startup and shutdown script for ChatterServer
VERSION=1.0.0-snapshot
PIDFILE=./pid/chatter.pid
SERVER=./chatter-$VERSION\.jar
ARGS="-Xms2g -Xmx4g -Xmn4g -Xss128k -XX:MaxPermSize=64m -XX:-UseParallelGC -XX:+UseParallelOldGC -XX:ParallelGCThreads=4 -XX:+UseConcMarkSweepGC -XX:MaxTenuringThreshold=30 -XX:SurvivorRatio=6"
# -Xms2g -Xmx2g -Xmn2g -Xss128k -XX:MaxPermSize=64m -XX:-UseParallelGC -XX:+UseParallelOldGC -XX:ParallelGCThreads=4 -XX:+UseConcMarkSweepGC -XX:MaxTenuringThreshold=30 -XX:SurvivorRatio=6
start()
{
if test -f $PIDFILE
then
if test -d /proc/$(cat $PIDFILE)
then
echo "server has started"
exit 1
fi
fi
if test -e $SERVER
then
echo "Starting $SERVER"
if java -jar $SERVER $ARGS >>./logs/console.out 2>&1 &
then
sleep 2
if [[ -d /proc/$! ]]; then
echo $! > $PIDFILE
echo "server start OK"
fi
elif java -jar $SERVER $ARGS >>./logs/console.out 2>&1 &
then
sleep 2
if [[ -d /proc/$! ]]; then
echo $! > $PIDFILE
echo "server start OK"
fi
else
echo "server start failed"
exit 1
fi
else
echo "Couldn‘t find $SERVER"
exit 1
fi
}
stop()
{
if test -e $PIDFILE
then
echo "Stopping server"
if kill -TERM `cat $PIDFILE` >>./logs/console.out 2>&1
then
sleep 2
if test -d /proc/$(cat $PIDFILE)
then
sleep 60
else
echo "server stop OK"
/bin/rm $PIDFILE
fi
elif
kill -KILL `cat $PIDFILE` >>./logs/console.out 2>&1
then
sleep 2
if test -d /proc/$(cat $PIDFILE)
then
sleep 60
else
echo "server stop OK"
/bin/rm $PIDFILE
fi
elif
kill -9 `cat $PIDFILE` >>./logs/console.out 2>&1
then
while [[ -d /proc/$(cat $PIDFILE) ]]; do
sleep 1
done
if test -d /proc/$(cat $PIDFILE)
then
echo "server stop failed"
exit 1
else
echo "server stop OK"
/bin/rm $PIDFILE
fi
else
echo "server stop failed"
exit 1
fi
else
echo "No server running"
exit 1
fi
}
restart()
{
echo "Restarting server"
stop
start
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo "Usage: $0 {start|stop|restart}" >&2
exit 1
;;
esac
exit 0--end--
====================================分割线======================================
运维经验分享作为一个专题,目前共7篇文章
本文出自 “通信,我的最爱” 博客,请务必保留此出处http://dgd2010.blog.51cto.com/1539422/1677157
运维经验分享(五)-- 改进的java进程管理的服务控制脚本
标签:服务管理 java进程管理 平滑重启java进程 进程信号 启动控制java服务进程
原文地址:http://dgd2010.blog.51cto.com/1539422/1677157