一、创建脚本文件
1、脚本文件:app.sh
#!/bin/bash
#建议使用 . xx.sh 命令执行脚本。如果使用sh xx.sh执行,注意is_exist方法里的注释
#获取进程名称,必须为完整程序名,否则可能会误操作其他进程
APP_NAME=app.jar
usage(){
echo "Usage: sh app.sh [start|stop|restart|status]"
exit 1
}
is_exist(){
#过滤grep命令本身
#注意②
pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' `
#使用sh xx.sh命令执行的话,启用下面代码
if [ -z "${pid}" ]; then
return 1
else
return 0
fi
}
start(){
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is already running. pid=${pid} ."
else
nohup java -jar $APP_NAME > nohup.log 2>&1 &
echo "${APP_NAME} start success"
fi
}
stop(){
is_exist
if [ $? -eq "0" ]