Linux 下 JAVA执行命令批处理方式
注意1,window下编辑的文件,linux上用不了,文件是以.sh结尾的
需要
1、vim xxxx.sh
2、:set ff unix
3、:wq!
注意2,
文件创建后需要授权,不然要报错没法用
chmod +x xxxx.sh
#r包路径,加不加引号都行。 注意:等号两边 不能 有空格,否则会提示command找不到 JAR_NAME1="/home/api/xxxx.jar" #JAR_NAME2="admin-service/admin-service.jar" # 日志路径,加不加引号都行。 注意:等号两边 不能 有空格,否则会提示command找不到 LOG_PATh=logs.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的状态更新 pid1=`ps -ef | grep $JAR_NAME1 | grep -v grep | awk '{print $2}'` # pid2=`ps -ef | grep $JAR_NAME2 | grep -v grep | awk '{print $2}'` # -z 表示如果$pid为空时执行 if [ -z $pid1 ]; then ./$JAR_NAME1 start -force fi # if [ -z $pid2 ]; then # nohup java -jar $JAR_NAME2 & # echo "启动${JAR_NAME2}" # 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的状态更新 pid1=`ps -ef | grep $JAR_NAME1 | grep -v grep | awk '{print $2}'` # pid2=`ps -ef | grep $JAR_NAME2 | grep -v grep | awk '{print $2}'` # -z 表示如果$pid为空时执行。 注意:每个命令和变量之间一定要前后加空格,否则会提示command找不到 if [ ! -z $pid1 ]; then kill -9 $pid1 fi # if [ ! -z $pid2 ]; then # kill -9 $pid2 # fi start echo "....................Restart successfully!..........................." } # 根据输入参数执行对应方法,不输入则执行tips提示方法 case "$1" in "start") start ;; "stop") stop ;; "status") status ;; "restart") restart ;; *) # tips stop sleep 1s start ;; esac
# -z 表示如果$pid为空时执行if [ -z $pid1 ]; then ./$JAR_NAME1 start -forcefi#if [ -z $pid2 ]; then# nohup java -jar $JAR_NAME2 &# echo "启动${JAR_NAME2}"#fi}# 停止方法stop() {# 重新获取一下pid,因为其它操作如start会导致pid的状态更新pid=`ps -ef | grep $JAR_NAME | grep -v grep | awk '{print $2}'` # -z 表示如果$pid为空时执行。 注意:每个命令和变量之间一定要前后加空格,否则会提示command找不到if [ -z $pid ]; thenecho "" echo "Service ${JAR_NAME} is not running! It's not necessary to stop it!"echo ""elsekill -9 $pidecho ""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 ];thenecho "" echo "Service ${JAR_NAME} is not running!"echo ""elseecho "" echo "Service ${JAR_NAME} is running. It's pid=${pid}"echo ""fi}# 重启方法restart() {echo ""echo ".............................Restarting.............................."echo "....................................................................."# 重新获取一下pid,因为其它操作如start会导致pid的状态更新pid1=`ps -ef | grep $JAR_NAME1 | grep -v grep | awk '{print $2}'`#pid2=`ps -ef | grep $JAR_NAME2 | grep -v grep | awk '{print $2}'`
# -z 表示如果$pid为空时执行。 注意:每个命令和变量之间一定要前后加空格,否则会提示command找不到if [ ! -z $pid1 ]; thenkill -9 $pid1fi#if [ ! -z $pid2 ]; then#kill -9 $pid2#fistartecho "....................Restart successfully!..........................."}# 根据输入参数执行对应方法,不输入则执行tips提示方法case "$1" in "start") start ;; "stop") stop ;; "status") status ;; "restart") restart ;; *)# tipsstopsleep 1sstart ;;esac
zzzzzzzzzzzz