#!/bin/bash
#定义一些用户变量
APP_HOME=/home/work/project/sys #程序主目录
APP_NAME=project-admin.jar #程序(脚本目录)
LOG_FILE=out.log #输出日志文件
#使用说明,用来提示输入参数
usage() {
echo "Usage: sh startup.sh [start|stop|restart|status|log]"
exit 1
}
#检查程序是否在运行
is_exist(){
pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' `
#如果不存在返回1,存在返回0
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_HOME/$APP_NAME >$APP_HOME/$LOG_FILE 2>&1 &
tail -f $APP_HOME/$LOG_FILE
echo "${APP_NAME} start success"
fi
}
#停止
stop(){
is_exist
if [ $? -eq "0" ]; then
kill -9 $pid
else
echo "${APP_NAME} is not running"
fi
}
#打印日志
log(){
is_exist
if [ $? -eq "0" ]; then
tail -f $APP_HOME/$LOG_FILE
else
echo "${APP_NAME} is not running"
fi
}
#输出运行状态
status(){
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is running. Pid is ${pid}"
else
echo "${APP_NAME} is NOT running."
fi
}
#重启
restart(){
stop
start
}
#根据输入参数,选择执行对应方法,不输入则显示使用说明
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
"log")
log
;;
*)
usage
;;
esac
第二种方式:
tomcat_home=/opt/usr/local/tomcat-8.5.57
SHUTDOWN=$tomcat_home/bin/shutdown.sh
STARTTOMCAT=$tomcat_home/bin/startup.sh
echo $2 $1 $tomcat_home
#pidlist1=`ps -ef |grep tomcat |grep -v "grep"|awk '{print $2}'
#echo $pidlis1t1
case $1 in
start)
echo "start $tomcat_home"
$STARTTOMCAT
tail -f $tomcat_home/logs/catalina.out
;;
stop)
echo "stop $tomcat_home"
$SHUTDOWN
pidlist=`ps -ef |grep tomcat |grep -v "grep"|awk '{print $2}'`
kill -9 $pidlist
#删除日志文件,如果你不先删除可以不要下面一行
rm $tomcat_home/logs/* -rf
#删除tomcat的临时目录
rm $tomcat_home/work/* -rf
;;
restart)
echo "stop $tomcat_home"
$SHUTDOWN
pidlist=`ps -ef |grep tomcat |grep -v "grep"|awk '{print $2}'`
kill -9 $pidlist
#删除日志文件,如果你不先删除可以不要下面一行
rm $tomcat_home/logs/* -rf
#删除tomcat的临时目录
rm $tomcat_home/work/* -rf
sleep 5
echo "start $tomcat_home"
$STARTTOMCAT
#看启动日志
tail -f $tomcat_home/logs/catalina.out
;;
logs)
cd $tomcat_home/logs
tail -f catalina.out
;;
执行出现:-bash: ./startup.sh: Permission denied
执行命令:chmod u+x *.sh