linux 条件语句关键词,Linux中条件语句

Linux中备份一个jar包,需要既判断进程是否停止,又判断文件是否存在

1. 进程停了,文件存在则备份

2.进程停了,文件不存在,无需备份

3. 进程在跑,停止进程;文件存在,将文件进行备份

4. 进程在跑,停止进程;文件不存在,无需备份

backup)

PID=$(ps -ef | grep ${APP_NAME} | grep -v grep | awk '{print $2}')

if [ -z "$PID" ] ;then

echo "${APP_NAME} is not running,will backup files"

if [ -e ${APP_NAME}];then

mv ${APP_HOME}/${APP_NAME} ${APP_HOME}/backup/${APP_NAME}-$(date +%Y%m%d%H%M%S)

else

echo "stop ${APP_NAME}"

kill -9 $PID

echo "will backup files..."

if [ -e ${APP_NAME}];then

mv ${APP_HOME}/${APP_NAME} ${APP_HOME}/backup/${APP_NAME}-$(date +%Y%m%d%H%M%S)

fi

;;

backup)

PID=$(ps -ef | grep ${APP_NAME} | grep -v grep | awk '{print $2}')

if [ -z "$PID" ] ;then

echo "${APP_NAME} is not running,will backup files"

if [ -e ${APP_NAME}];then

mv ${APP_HOME}/${APP_NAME} ${APP_HOME}/backup/${APP_NAME}-$(date +%Y%m%d%H%M%S)

else

echo "stop ${APP_NAME}"

kill -9 $PID

echo "will backup files..."

if [ -e ${APP_NAME}];then

mv ${APP_HOME}/${APP_NAME} ${APP_HOME}/backup/${APP_NAME}-$(date +%Y%m%d%H%M%S)

fi

;;

忽然又觉得为啥要先判断进程呢,先判断文件啊~~~~

文件存在,进程在,杀进程,备份文件

文件存在,进程不再,备份文件

文件不存在,无需操作

就少了一步

保存下

#!/bin/bash

#server 10.215.65.131

JAVA=/usr/java/jdk1.8.0_211-amd64/bin/java

APP_HOME=/opt/xxx/xxx-xxx

APP_NAME=xxx-xxx.jar

APP_PARAM="--spring.config.location=${APP_HOME}/application.properties"

case $1 in

start)

PID=$(ps -ef | grep ${APP_NAME} | grep -v grep | awk '{print $2}')

if [ -z "$PID" ] ;then

echo "start ${APP_NAME}"

nohup ${JAVA} -jar ${APP_HOME}/${APP_NAME} ${APP_PARAM} > ${APP_HOME}/catalina.out 2>&1 &

else

echo "${APP_NAME} is running"

fi

;;

stop)

PID=$(ps -ef | grep ${APP_NAME} | grep -v grep | awk '{print $2}')

if [ -z "$PID" ] ;then

echo "${APP_NAME} is not running"

else

echo "stop ${APP_NAME}"

kill -9 $PID

fi

;;

restart)

PID=$(ps -ef | grep ${APP_NAME} | grep -v grep | awk '{print $2}')

if [ -z "$PID" ] ;then

echo "start ${APP_NAME}"

nohup ${JAVA} -jar ${APP_HOME}/${APP_NAME} ${APP_PARAM} > ${APP_HOME}/catalina.out 2>&1 &

else

echo "stop ${APP_NAME}"

kill -9 $PID

echo "start ${APP_NAME}"

nohup ${JAVA} -jar ${APP_HOME}/${APP_NAME} ${APP_PARAM} > ${APP_HOME}/catalina.out 2>&1 &

fi

;;

backup)

PID=$(ps -ef | grep ${APP_NAME} | grep -v grep | awk '{print $2}')

if [ -z "$PID" ] ;then

echo "${APP_NAME} is not running,will backup files"

if [ -e ${APP_NAME}];then

mv ${APP_HOME}/${APP_NAME} ${APP_HOME}/backup/${APP_NAME}-$(date +%Y%m%d%H%M%S)

else

echo "stop ${APP_NAME}"

kill -9 $PID

echo "will backup files..."

if [ -e ${APP_NAME}];then

mv ${APP_HOME}/${APP_NAME} ${APP_HOME}/backup/${APP_NAME}-$(date +%Y%m%d%H%M%S)

fi

;;

logs)

tail -f ${APP_HOME}/catalina.out

;;

status)

PID=$(ps -ef | grep ${APP_NAME} | grep -v grep | awk '{print $2}')

if [ -z "$PID" ] ;then

echo "${APP_NAME} is not running"

else

echo "${APP_NAME} is running, pid $PID"

fi

;;

*)

echo "您输入的指令没有对应的方法哟"

esac

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值