在Linux中创建脚本命令启动Jar
1、创建run.sh
$ touch run.sh
2、run.sh
#!/bin/bash
APP_NAME=you-project-name.jar
PROFILE_ACTIVE=test
start_command(){
nohup java -server -Xms500M -Xmx1024M -Xss512k \
-Dspring.profiles.active=${PROFILE_ACTIVE} \
-Dspring.redis.host='' \
-Dspring.redis.password='' \
-jar ${APP_NAME} > catalina.out 2>&1 &
}
usage() {
echo "Usage: sh run.sh [start|stop|restart|status]"
exit 1
}
is_exist(){
pid=$(ps -ef | grep ${APP_NAME} | grep -v grep | awk '{print $2}')
if [ -z "${pid}" ]; then
return 1
else
return 0
fi
}
start(){
echo "start running ${APP_NAME} ............... "
is_exist
if [ $? -eq 0 ]; then
echo "${APP_NAME} is already running. Pid is ${pid}"
else
start_command
echo "${APP_NAME} start success........."
tail -500f ./catalina.out
fi
}
stop(){
echo "stopping running ${APP_NAME} ............... "
is_exist
if [ $? -eq "0" ]; then
kill -9 $pid
echo "${APP_NAME} is kill, Pid is $pid"
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
sleep 5s
start
}
case "$1" in
"start")
start
;;
"stop")
stop
;;
"restart")
restart
;;
"status")
status
;;
*)
usage
;;
esac
3、操作命令
$ cd /usr/local/project/
$ sh run.sh start
$ cd /usr/local/project/
$ sh run.sh stop
$ cd /usr/local/project/
$ sh run.sh restart
$ cd /usr/local/project/
$ sh run.sh status
$ tail -500f catalina.out