看到比较好的linux脚本:
start.sh
#!/bin/sh
rm -f tpid
nohup java -jar xx.jar --spring.profiles.active=dev > /dev/null 2>&1 &
echo $! > tpid
echo Start Success!
stop.sh
#!/bin/sh
APP_NAME=myapp
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
echo 'Stop Process...'
kill -15 $tpid
fi
sleep 5
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
echo 'Kill Process!'
kill -9 $tpid
else
echo 'Stop Success!'
fi
check.sh
#!/bin/sh
APP_NAME=myapp
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
echo 'App is running.'
else
echo 'App is NOT running.'
fi
kill.sh
#!/bin/sh
APP_NAME=myapp
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
echo 'Kill Process!'
kill -9 $tpid
fi
以上合并为一个脚本:
#!/bin/sh
APP=stockhouse
APP_NAME=${APP}".jar"
command=$1
function start()
{
rm -f tpid
nohup java -jar ${APP_NAME} > /dev/null 2>&1 &
echo $! > tpid
check
}
function stop()
{
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
echo 'Stop Process...'
kill -15 $tpid
fi
sleep 5
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
echo 'Kill Process!'
kill -9 $tpid
else
echo 'Stop Success!'
fi
}
function check()
{
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
echo 'App is running.'
else
echo 'App is NOT running.'
fi
}
function forcekill()
{
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
echo 'Kill Process!'
kill -9 $tpid
fi
}
if [ "${command}" == "start" ]; then
start
elif [ "${command}" == "stop" ]; then
stop
elif [ "${command}" == "check" ]; then
check
elif [ "${command}" == "kill" ]; then
forcekill
else
echo "Unknow argument...."
fi
Java应用一键管理脚本
本文分享了一套用于管理Java应用程序的Linux shell脚本,包括启动(start)、停止(stop)、检查状态(check)及强制终止(kill)等功能,极大地简化了Java应用的部署与维护流程。

555

被折叠的 条评论
为什么被折叠?



