#!/usr/bin/bash usage(){ echo usage ./shellfile option appfilename echo options:[stop/start/retsart/backup] } appstop(){ if [ -f $1 ] then echo to stop app with:$1 filename=$1 pids=`jps -mlvV | grep ${filename##*/} | awk '{print $1}'` for i in $pids do kill $i if [ $? == 0 ] then echo quiet exit ok! else kill -9 $i echo force exit ok! fi done else echo file:$1 is not found fi } appstart(){ if [ -f $1 ] then filename=$1 #后台运行java程序 pid=`nohup java -jar ${filename##*/} > ./nohup.out 2>&1 &` if [ $? == 0 ] then echo ok else echo error fi else echo file:$1 is not found fi } backup(){ if [ -f $1 ] then now=`date '+%Y%m%dT%H%M%S'` `cp $1 $1.back.$now` else echo file:$1 is not found fi } apprestart(){ filename=$1 # 找到目标进程 截取文件名称 获取PID pids=`jps -mlvV | grep ${filename##*/} | awk '{print $1}'` if [ $pids ] then appstop $filename #脚本暂停5秒 sleep 5 appstart $filename else appstart $filename fi } case $1 in stop) appstop $2 ;; start) appstart $2 ;; restart) apprestart $2 ;; backup) backup $2 ;; *) usage ;; esac
nohup: 重定向标准错误到标准输出:https://blog.csdn.net/x18094/article/details/106526738
shell字符串截取函数参考:http://c.biancheng.net/view/1120.html
shell日期格式化参考:https://blog.csdn.net/stone_tomcate/article/details/122193554