假设java程序的jar包是monserver.jar ,存放路径/usr/local/monserver/monserver.jar
在目录/etc/rc.d/init.d/中创建脚本monserver
/etc/rc.d/init.d/monserver
#! /bin/sh
command='java -Xms512m -Xmx4096m -jar /usr/local/monserver/monserver.jar'
#启动方法
start(){
exec $command &
}
#停止方法
stop(){
ps -ef | grep "$command" | awk '{print $2}' | while read pid
do
C_PID=$(ps --no-heading $pid | wc -l)
echo "PID = $pid"
if [ "$C_PID" == "1" ]; then
echo "PID = $pid ready for stop......"
kill -9 $pid
echo "PID = $pid stoped!"
else
echo "PID = $pid unexisted!"
fi
done
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
printf 'Usage: %s {start|stop|restart}\n' "$prog"
exit 1
;;
esac
启动命令:service monserver start
停止命令:service monserver stop
重启命令:service monserver restart