#!/bin/bash
appName=`ls|grep .jar$`
if [ -z $appName ]
then
echo "Please check that this script and your jar-package is in the same directory!"
exit 1
fi
killForceFlag=$2
function start()
{
count=`ps -ef |grep java|grep $appName|wc -l`
if [ $count != 0 ];then
echo "cecc服务已启动,请勿重复启动!!!"
else
echo "cecc服务启动成功!!!"
nohup java -XX:+UseG1GC -XX:+HeapDumpOnOutOfMemoryError -Xms512M -Xmx4G -jar $appName > /dev/null 2>&1 &
fi
}
function stop()
{
appId=`ps -ef |grep java|grep $appName|awk '{print $2}'`
if [ -z $appId ]
then
echo "cecc服务已停止,请勿重复停止!!!"
else
echo -n "cecc服务已停止"
if [ "$killForceFlag" == "-f" ]
then
echo "by force"
kill -9 $appId
else
echo
kill $appId
fi
fi
}
function status()
{
appId=`ps -ef |grep java|grep $appName|awk '{print $2}'`
if [ -z $appId ]
then
echo -e "\033[31m Not running \033[0m"
else
echo -e "\033[32m Running [$appId] \033[0m"
fi
}
function restart()
{
stop
for i in {3..1}
do
echo -n "$i "
sleep 1
done
echo 0
start
}
function usage()
{
echo "Usage: $0 {start|stop|restart|status|stop -f}"
echo "Example: $0 start"
exit 1
}
case $1 in
start)
start;;
stop)
stop;;
restart)
restart;;
status)
status;;
*)
usage;;
esac
start代表启动,stop代表停止,restart代表重启
首先,将你需要发布的jar包,和含有上述内容的脚本xxx.sh,上传至linux服务器,注意两者必须处于同一目录,并且该目录下只有一个jar包,并给与xxx.sh相应执行权限,chmod 777 xxx.sh
如果出现,Linux环境执行脚本报错:-bash: ./xxx.sh: /bin/bash^M: 坏的解释器: 没有那个文件或目录,则只需要执行 sed -i 's/\r$//' xxxxx.sh 即可