linux(restart stop status start)自动启动项目并配置日志(一)

#!/bin/bash
# 定义变量
# 要运行的jar包路径,加不加引号都行。 注意:等号两边 不能 有空格,否则会提示command找不到
JAR_NAME=vcxcx.jar
# 日志路径,加不加引号都行。 注意:等号两边 不能 有空格,否则会提示command找不到
LOG_PATh=/opt/xcx/vcxcx_log/vcxcx-$(date +%Y-%m-%d).log
 
 
# 如果输入格式不对,给出提示!
tips() {
	echo ""
	echo "WARNING!!!......Tips, please use command: sh auto_deploy.sh [start|stop|restart|status].   For example: sh auto_deploy.sh start  "
	echo ""
	exit 1
}
 
 
# 启动方法
start() {
        # 重新获取一下pid,因为其它操作如stop会导致pid的状态更新
	pid=`ps -ef | grep $JAR_NAME | grep -v grep | awk '{print $2}'`
        # -z 表示如果$pid为空时执行
	if [ -z $pid ]; then
		nohup java -jar app_name.jar >> /home/log/app_name-$(date +%Y-%m-%d).log 2>&1 &
        pid=`ps -ef | grep $JAR_NAME | grep -v grep | awk '{print $2}'`
		echo ""
        echo "Service ${JAR_NAME} is starting!pid=${pid}"
		echo "........................Here is the log.............................."
		echo "....................................................................."
        tail -f $LOG_PATh
		echo "........................Start successfully!........................."
	else
		echo ""
		echo "Service ${JAR_NAME} is already running,it's pid = ${pid}. If necessary, please use command: sh auto_deploy.sh restart."
		echo ""
	fi
}
 
# 停止方法
stop() {
		# 重新获取一下pid,因为其它操作如start会导致pid的状态更新
	pid=`ps -ef | grep $JAR_NAME | grep -v grep | awk '{print $2}'`
        # -z 表示如果$pid为空时执行。 注意:每个命令和变量之间一定要前后加空格,否则会提示command找不到
	if [ -z $pid ]; then
		echo ""
        echo "Service ${JAR_NAME} is not running! It's not necessary to stop it!"
		echo ""
	else
		kill -9 $pid
		echo ""
		echo "Service stop successfully!pid:${pid} which has been killed forcibly!"
		echo ""
	fi
}
 
# 输出运行状态方法
status() {
        # 重新获取一下pid,因为其它操作如stop、restart、start等会导致pid的状态更新
	pid=`ps -ef | grep $JAR_NAME | grep -v grep | awk '{print $2}'`
        # -z 表示如果$pid为空时执行。注意:每个命令和变量之间一定要前后加空格,否则会提示command找不到
	if [ -z $pid ];then
		echo ""
        echo "Service ${JAR_NAME} is not running!"
		echo ""
	else
		echo ""
        echo "Service ${JAR_NAME} is running. It's pid=${pid}"
		echo ""
	fi
}
 
# 重启方法
restart() {
	echo ""
	echo ".............................Restarting.............................."
	echo "....................................................................."
		# 重新获取一下pid,因为其它操作如start会导致pid的状态更新
	pid=`ps -ef | grep $JAR_NAME | grep -v grep | awk '{print $2}'`
        # -z 表示如果$pid为空时执行。 注意:每个命令和变量之间一定要前后加空格,否则会提示command找不到
	if [ ! -z $pid ]; then
		kill -9 $pid
	fi
	start
	echo "....................Restart successfully!..........................."
}
 
# 根据输入参数执行对应方法,不输入则执行tips提示方法
case "$1" in
   "start")
     start
     ;;
   "stop")
     stop
     ;;
   "status")
     status
     ;;
   "restart")
     restart
     ;;
   *)
     tips
     ;;


esac

下面是关于jeecgsite的启动修改:

#!/bin/bash

日志路径,加不加引号都行。 注意:等号两边 不能 有空格,否则会提示command找不到

echo “”
echo “[信息] 运行Web工程。”
echo “”

LOG_PATh=/opt/xcxAdmin/previousLogs/$(date +%Y-%m-%d).log
PORT=8988

如果输入格式不对,给出提示!

tips() {
echo “”
echo "WARNING!!!..Tips, please use command: sh auto_deploy.sh [start|stop|restart|status]. For example: sh auto_deploy.sh start "
echo “”
exit 1
}

设置JDK目录

JAVA_HOME="$PWD/jdk1.8.0_x64"

设置类加载路径

CLASS_PATH="$PWD/…/"

优化JVM参数

JAVA_OPTS="-Xms512m -Xmx1024m"

方式一、配置外部自定义的属性文件(建议)

JAVA_OPTS=“ J A V A O P T S − D s p r i n g . c o n f i g . l o c a t i o n = JAVA_OPTS -Dspring.config.location= JAVAOPTSDspring.config.location=PWD/app.yml”

方式二、配置环境名称,加载不同的属性文件

JAVA_OPTS="$JAVA_OPTS -Dspring.profiles.active=prod"

if [ -z “ J A V A H O M E " ] ; t h e n R U N J A V A = j a v a e l s e R U N J A V A = " JAVA_HOME" ]; then RUN_JAVA=java else RUN_JAVA=" JAVAHOME"];thenRUNJAVA=javaelseRUNJAVA="JAVA_HOME”/bin/java
fi

启动方法

start() {
# 重新获取一下pid,因为其它操作如stop会导致pid的状态更新
pid=KaTeX parse error: Undefined control sequence: \< at position 24: … -nlp | grep :'\̲<̲'PORT’>’ | awk ‘{print $7}’ | awk -F"/" ‘{ print KaTeX parse error: Expected 'EOF', got '}' at position 3: 1 }̲') # -z…pid为空时执行
if [ -z $pid ]; then
exec $RUN_JAVA -cp $CLASS_PATH $JAVA_OPTS org.springframework.boot.loader.WarLauncher >> KaTeX parse error: Expected 'EOF', got '&' at position 12: LOG_PATh 2>&̲1 & pid=(netstat -nlp | grep :’<’$PORT’>’ | awk ‘{print $7}’ | awk -F"/" '{ print KaTeX parse error: Expected 'EOF', got '}' at position 3: 1 }̲') echo "" …{pid}"
echo “…Here is the log…”
echo “…”
tail -f $LOG_PATh
echo “…Start successfully!…”
else
echo “”
echo “Service xcxAdmin is already running,it’s pid = ${pid}. If necessary, please use command: sh auto_deploy.sh restart.”
echo “”
fi
}

停止方法

stop() {
# 重新获取一下pid,因为其它操作如start会导致pid的状态更新
pid=KaTeX parse error: Undefined control sequence: \< at position 24: … -nlp | grep :'\̲<̲'PORT’>’ | awk ‘{print $7}’ | awk -F"/" '{ print KaTeX parse error: Expected 'EOF', got '}' at position 3: 1 }̲') # -z…pid为空时执行。 注意:每个命令和变量之间一定要前后加空格,否则会提示command找不到
if [ -z $pid ]; then
echo “”
echo “Service xcxAdmin is not running! It’s not necessary to stop it!”
echo “”
else
kill -9 p i d e c h o " " e c h o " S e r v i c e s t o p s u c c e s s f u l l y ! p i d : pid echo "" echo "Service stop successfully!pid: pidecho""echo"Servicestopsuccessfullypid:{pid} which has been killed forcibly!"
echo “”
fi
}

输出运行状态方法

status() {
# 重新获取一下pid,因为其它操作如stop、restart、start等会导致pid的状态更新
pid=KaTeX parse error: Undefined control sequence: \< at position 24: … -nlp | grep :'\̲<̲'PORT’>’ | awk ‘{print $7}’ | awk -F"/" '{ print KaTeX parse error: Expected 'EOF', got '}' at position 3: 1 }̲') # -z…pid为空时执行。注意:每个命令和变量之间一定要前后加空格,否则会提示command找不到
if [ -z p i d ] ; t h e n e c h o " " e c h o " S e r v i c e x c x A d m i n i s n o t r u n n i n g ! " e c h o " " e l s e e c h o " " e c h o " S e r v i c e x c x A d m i n i s r u n n i n g . I t ′ s p i d = pid ];then echo "" echo "Service xcxAdmin is not running!" echo "" else echo "" echo "Service xcxAdmin is running. It's pid= pid];thenecho""echo"ServicexcxAdminisnotrunning!"echo""elseecho""echo"ServicexcxAdminisrunning.Itspid={pid}"
echo “”
fi
}

重启方法

restart() {
echo “”
echo “…Restarting…”
echo “…”
# 重新获取一下pid,因为其它操作如start会导致pid的状态更新
pid=KaTeX parse error: Undefined control sequence: \< at position 24: … -nlp | grep :'\̲<̲'PORT’>’ | awk ‘{print $7}’ | awk -F"/" '{ print KaTeX parse error: Expected 'EOF', got '}' at position 3: 1 }̲') # -z…pid为空时执行。 注意:每个命令和变量之间一定要前后加空格,否则会提示command找不到
if [ ! -z $pid ]; then
kill -9 $pid
fi
start
echo “…Restart successfully!…”
}

根据输入参数执行对应方法,不输入则执行tips提示方法

case “$1” in
“start”)
start
;;
“stop”)
stop
;;
“status”)
status
;;
“restart”)
restart
;;
*)
tips
;;

esac


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

健身小白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值