jar 启动关闭

1.后台启动 startTest.sh

#设置工程路径
project_path=/root/test
cd $project_path
#nohup后台启动,输出日志到test.log
nohup java -jar test.jar >test.log &
#打印日志
tail -f test.log
文件可执行权

chmod +x startTest.sh

执行 ./startTest.sh

2.根据应用端口关闭服务 stopTest.sh

#设置关闭的端口
port=8080
#获取此端口运行的进程
pid=`lsof -t -i:$port`
#判断如果进程号不为空则,关闭进程
if test -z "$pid";then
echo "test 工程未启动!"
else
kill -9 $pid
echo "test 工程进程$pid 关闭成功!"
文件赋执行权同上
---------------------

#!/bin/bash
#这里可替换为你自己的执行程序,其他代码无需更改

APP_NAME=/application/yutianyu_test/wechat-activities-1.0-SNAPSHOT-jar-with-dependencies.jar
#使用说明,用来提示输入参数
usage() {
echo "Usage: sh robotcenter.sh [start|stop|restart|status]"
exit 1
}

#检查程序是否在运行
is_exist(){
pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}'`
#如果不存在返回1,存在返回0
if [ -z "${pid}" ]; then
return 1
else
return 0
fi
}

#启动方法
start(){
is_exist
if [ $? -eq 0 ]; then
echo "${APP_NAME} is already running. pid=${pid}"
else
nohup java -jar ${APP_NAME} >robotcenter.out 2>&1 &
fi
}

#停止方法
stop(){
is_exist
if [ $? -eq "0" ]; then
kill -9 $pid
else
echo "${APP_NAME} is not running"
fi
}

#输出运行状态
status(){
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is running. Pid is ${pid}"
else
echo "${APP_NAME} is NOT running."
fi
}

#重启
restart(){
stop
sleep 5
start
}

#根据输入参数,选择执行对应方法,不输入则执行使用说明
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
usage
;;
esac



猜你喜欢

转载自www.cnblogs.com/zdqc/p/10043552.html