1.服务器启用/停止项目脚本
#!/bin/bash
cd `dirname $0`
CUR_SHELL_DIR=`pwd`
CUR_SHELL_NAME=`basename ${BASH_SOURCE}`
#修改这里jar包名即可
JAR_NAME="xxl-job-admin-2.3.0.jar"
JAR_PATH=$CUR_SHELL_DIR/$JAR_NAME
#JAVA_MEM_OPTS=" -server -Xms1024m -Xmx1024m -XX:PermSize=128m"
JAVA_MEM_OPTS=""
#SPRING_PROFILES_ACTIV="-Dspring.profiles.active=dev"
SPRING_PROFILES_ACTIV=""
LOG_DIR=$CUR_SHELL_DIR/logs
LOG_PATH=$LOG_DIR/${JAR_NAME}.log
echo_help()
{
echo -e "syntax: sh $CUR_SHELL_NAME start|stop"
}
if [ -z $1 ];then
echo_help
exit 1
fi
if [ ! -d "$LOG_DIR" ];then
mkdir "$LOG_DIR"
fi
if [ ! -f "$LOG_PATH" ];then
touch "$LOG_DIR"
fi
if [ "$1" == "start" ];then
# check server
PIDS=`ps --no-heading -C java -f --width 1000 | grep $JAR_NAME | awk '{print $2}'`
if [ -n "$PIDS" ]; then
echo -e "ERROR: The $JAR_NAME already started and the PID is ${PIDS}."
exit 1
fi
echo "Starting the $JAR_NAME..."
# start
nohup java $JAVA_MEM_OPTS -jar $SPRING_PROFILES_ACTIV $JAR_PATH >> $LOG_PATH 2>&1 &
COUNT=0
while [ $COUNT -lt 1 ]; do
sleep 1
COUNT=`ps --no-heading -C java -f --width 1000 | grep "$JAR_NAME" | awk '{print $2}' | wc -l`
if [ $COUNT -gt 0 ]; then
break
fi
done
PIDS=`ps --no-heading -C java -f --width 1000 | grep "$JAR_NAME" | awk '{print $2}'`
echo "${JAR_NAME} Started and the PID is ${PIDS}."
echo "You can check the log file in ${LOG_PATH} for details."
elif [ "$1" == "stop" ];then
PIDS=`ps --no-heading -C java -f --width 1000 | grep $JAR_NAME | awk '{print $2}'`
if [ -z "$PIDS" ]; then
echo "ERROR:The $JAR_NAME does not started!"
exit 1
fi
echo -e "Stopping the $JAR_NAME..."
for PID in $PIDS; do
kill $PID > /dev/null 2>&1
done
COUNT=0
while [ $COUNT -lt 1 ]; do
sleep 1
COUNT=1
for PID in $PIDS ; do
PID_EXIST=`ps --no-heading -p $PID`
if [ -n "$PID_EXIST" ]; then
COUNT=0
break
fi
done
done
echo -e "${JAR_NAME} Stopped and the PID is ${PIDS}."
else
echo_help
exit 1
fi
2.会遇见window 和linux 换行符不一样的错误
[root@iZu app]# ./run_xxl-job.sh start
-bash: ./run_xxl-job.sh: /bin/bash^M: 坏的解释器: 没有那个文件或目录
解决方案
Linux sed命令可自动编辑一个或多个文件,执行以下命令即可。
sed -i 's/\r$//' run_xxl-job.sh
另一个sh jar脚本
#!/bin/bash
# Java ENV
export JAVA_HOME=/usr/local/java/jdk1.8.0_211
export JRE_HOME=${JAVA_HOME}/jre
# Apps Info
# 应用存放地址
APP_HOME=/app/springboot/group-service
# 应用名称
APP_NAME=$1
# Shell Info
# 使用说明,用来提示输入参数
usage() {
echo "Usage: sh boot [APP_NAME] [start|stop|restart|status]"
exit 1
}
# 检查程序是否在运行
is_exist(){
# 获取PID
PID=$(ps -ef |grep ${APP_NAME} | grep -v $0 |grep -v grep |awk '{print $2}')
# -z "${pid}"判断pid是否存在,如果不存在返回1,存在返回0
if [ -z "${PID}" ]; then
# 如果进程不存在返回1
return 1
else
# 进程存在返回0
return 0
fi
}
# 定义启动程序函数
start(){
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is already running, PID=${PID}"
else
nohup ${JRE_HOME}/bin/java -server -Xms1g -Xmx1g -Xmn512m -XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=512m -XX:SurvivorRatio=6 -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=80 -XX:+ScavengeBeforeFullGC -XX:+CMSScavengeBeforeRemark -XX:+PrintGCDateStamps -verbose:gc -XX:+PrintGCDetails -Xloggc:gc.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=dump.hprof -jar ${APP_HOME}/${APP_NAME} --spring.profiles.active=pro ----server.port=9001 > /dev/null 2>&1 &
PID=$(echo $!)
echo "${APP_NAME} start success, PID=$!"
fi
}
# 停止进程函数
stop(){
is_exist
if [ $? -eq "0" ]; then
kill -9 ${PID}
echo "${APP_NAME} process stop, PID=${PID}"
else
echo "There is not the process of ${APP_NAME}"
fi
}
# 重启进程函数
restart(){
stop
start
}
# 查看进程状态
status(){
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is running, PID=${PID}"
else
echo "There is not the process of ${APP_NAME}"
fi
}
case $2 in
"start")
start
;;
"stop")
stop
;;
"restart")
restart
;;
"status")
status
;;
*)
usage
;;
esac
exit 0
另附常用的命令启动项目:
java -jar xxl-job-admin-2.3.0.jar --spring.profiles.active=sit --server.port=8088 -XX:+StartAttachListener
终止服务器正在运行的项目
ps -ef | grep java
找到xxxx.jar项目,记住root后面的pid号 ,杀死他
kill -s 9 pid
打包好项目再重新部署
nohup java -jar XXX.jar &
使用这种方式运行的程序日志会输出到当前目录下的nohup.out文件,使用ctrl+c中断或者关闭窗口都不会中断程序的执行。
如果遇到端口号被占用的,可以找出来再杀死(比如8089端口)
netstat -tln | grep 8089
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 13242 root 16u IPv4 3281449 0t0 TCP *:8089 (LISTEN)
找到PID杀死他
kill -9 13242
再次查看,看端口还被占用不
netstat -tln | grep 8089
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/65814.html