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
![](https://images.cnblogs.com/cnblogs_com/qianjinyan/910455/o_jasmine%20love.gif)