<finalName>${project.artifactId}_${project.version}_${current.time}</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>timestamp-property</id>
<goals>
<goal>timestamp-property</goal>
</goals>
<configuration>
<name>current.time</name>
<pattern>yyyyMMdd-HHmmss</pattern>
<timeZone>GMT+8</timeZone>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
run.sh
#!/bin/bash
# set APP name prefix
APP_PREFIX="real-people-parent"
# set path to Java binary
export JAVA_HOME=/data1/zulujdk17/bin/
export JRE_HOME=$JAVA_HOME/jre
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=./:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
# set logs directory
LOGS_DIR=./logs
# get PID file name
PID_FILE=./app.pid
# check if PID file exists
if [ -f "$PID_FILE" ]; then
# read the PID from the file
PID=$(cat "$PID_FILE")
else
# set the PID to 0 if the file does not exist
PID=0
fi
# check if the app is already running
is_running() {
if [ "$PID" -ne 0 ] && [ -d "/proc/$PID" ]; then
return 0
else
return 1
fi
}
# find the latest JAR file
find_latest_jar() {
latest_jar=$(ls -Art | grep "^$APP_PREFIX" | tail -n 1)
echo $latest_jar
}
# start the app
start() {
is_running
if [ $? -eq 0 ]; then
echo "App is already running with PID $PID"
exit 1
else
# find the latest JAR file
latest_jar=$(find_latest_jar)
if [ -z "$latest_jar" ]; then
echo "No JAR file found with prefix $APP_PREFIX"
exit 1
fi
# start the app
nohup "${JAVA_HOME}/java" -jar "$latest_jar" > "${LOGS_DIR}/log-nohup.out" 2>&1 &
PID=$!
echo "App started with PID $PID"
# write the PID to the PID file
echo "$PID" > "$PID_FILE"
fi
}
# stop the app
stop() {
is_running
if [ $? -eq 0 ]; then
kill "$PID"
echo "App stopped with PID $PID"
# remove the PID file
rm "$PID_FILE"
else
echo "App is not running"
fi
}
# restart the app
restart() {
is_running
if [ $? -eq 0 ]; then
stop
sleep 5
fi
start
}
# display the status of the app
status() {
is_running
if [ $? -eq 0 ]; then
echo "App is running with PID $PID"
else
echo "App is not running"
fi
}
# print usage information
usage() {
echo "Usage: $0 {start|stop|restart|status}"
exit 1
}
# check for the correct number of arguments
if [ $# -ne 1 ]; then
usage
fi
#根据输入参数,选择执行对应方法,不输入则执行使用说明
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
usage
;;
esac
#日志轮转,7天之前的日志进行压缩备份
if [ ! -d "$LOGS_DIR" ]; then
mkdir "$LOGS_DIR"
fi
cat > /etc/logrotate.d/app << EOF
${LOGS_DIR}/log-nohup.out {
daily
missingok
rotate 7
compress
delaycompress
notifempty
create
}
EOF