perf:更新新的脚本
This commit is contained in:
@@ -40,82 +40,11 @@ done
|
||||
jarfolder="$( (cd "$(dirname "$jarfile")" && pwd -P) )"
|
||||
cd "$WORKING_DIR" || exit 1
|
||||
|
||||
# Source any config file
|
||||
configfile="$(basename "${jarfile%.*}.conf")"
|
||||
|
||||
# Initialize CONF_FOLDER location defaulting to jarfolder
|
||||
[[ -z "$CONF_FOLDER" ]] && CONF_FOLDER="{{confFolder:${jarfolder}}}"
|
||||
|
||||
# shellcheck source=/dev/null
|
||||
[[ -r "${CONF_FOLDER}/${configfile}" ]] && source "${CONF_FOLDER}/${configfile}"
|
||||
|
||||
# Initialize PID/LOG locations if they weren't provided by the config file
|
||||
[[ -z "$PID_FOLDER" ]] && PID_FOLDER="{{pidFolder:/var/run}}"
|
||||
[[ -z "$LOG_FOLDER" ]] && LOG_FOLDER="{{logFolder:/var/log}}"
|
||||
! [[ "$PID_FOLDER" == /* ]] && PID_FOLDER="$(dirname "$jarfile")"/"$PID_FOLDER"
|
||||
! [[ "$LOG_FOLDER" == /* ]] && LOG_FOLDER="$(dirname "$jarfile")"/"$LOG_FOLDER"
|
||||
! [[ -x "$PID_FOLDER" ]] && PID_FOLDER="/tmp"
|
||||
! [[ -x "$LOG_FOLDER" ]] && LOG_FOLDER="/tmp"
|
||||
|
||||
# Set up defaults
|
||||
[[ -z "$MODE" ]] && MODE="{{mode:auto}}" # modes are "auto", "service" or "run"
|
||||
[[ -z "$USE_START_STOP_DAEMON" ]] && USE_START_STOP_DAEMON="{{useStartStopDaemon:true}}"
|
||||
|
||||
# Create an identity for log/pid files
|
||||
if [[ -z "$identity" ]]; then
|
||||
if [[ -n "$init_script" ]]; then
|
||||
identity="${init_script}"
|
||||
else
|
||||
identity=$(basename "${jarfile%.*}")_${jarfolder//\//}
|
||||
fi
|
||||
fi
|
||||
|
||||
# Initialize log file name if not provided by the config file
|
||||
[[ -z "$LOG_FILENAME" ]] && LOG_FILENAME="{{logFilename:${identity}.log}}"
|
||||
|
||||
# Initialize stop wait time if not provided by the config file
|
||||
[[ -z "$STOP_WAIT_TIME" ]] && STOP_WAIT_TIME="{{stopWaitTime:60}}"
|
||||
|
||||
# ANSI Colors
|
||||
echoRed() { echo $'\e[0;31m'"$1"$'\e[0m'; }
|
||||
echoGreen() { echo $'\e[0;32m'"$1"$'\e[0m'; }
|
||||
echoYellow() { echo $'\e[0;33m'"$1"$'\e[0m'; }
|
||||
|
||||
# Utility functions
|
||||
checkPermissions() {
|
||||
touch "$pid_file" &> /dev/null || { echoRed "Operation not permitted (cannot access pid file)"; return 4; }
|
||||
touch "$log_file" &> /dev/null || { echoRed "Operation not permitted (cannot access log file)"; return 4; }
|
||||
}
|
||||
|
||||
isRunning() {
|
||||
ps -p "$1" | grep -v "PID" &> /dev/null
|
||||
}
|
||||
|
||||
await_file() {
|
||||
end=$(date +%s)
|
||||
let "end+=10"
|
||||
while [[ ! -s "$1" ]]
|
||||
do
|
||||
now=$(date +%s)
|
||||
if [[ $now -ge $end ]]; then
|
||||
break
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
}
|
||||
|
||||
# Determine the script mode
|
||||
action="config"
|
||||
if [[ "$MODE" == "auto" && -n "$init_script" ]] || [[ "$MODE" == "service" ]]; then
|
||||
action="$1"
|
||||
shift
|
||||
fi
|
||||
|
||||
# Build the pid and log filenames
|
||||
PID_FOLDER="$PID_FOLDER/${identity}"
|
||||
pid_file="$PID_FOLDER/{{pidFilename:${identity}.pid}}"
|
||||
log_file="$LOG_FOLDER/$LOG_FILENAME"
|
||||
|
||||
# Determine the user to run as if we are root
|
||||
# shellcheck disable=SC2012
|
||||
[[ $(id -u) == "0" ]] && run_user=$(ls -ld "$jarfile" | awk '{print $3}')
|
||||
@@ -132,125 +61,39 @@ else
|
||||
exit 1
|
||||
fi
|
||||
|
||||
arguments=(-Dsun.misc.URLClassPath.disableJarChecking=true $JAVA_OPTS -jar "$jarfile" $RUN_ARGS "$@")
|
||||
|
||||
# Action functions
|
||||
start() {
|
||||
if [[ -f "$pid_file" ]]; then
|
||||
pid=$(cat "$pid_file")
|
||||
isRunning "$pid" && { echoYellow "Already running [$pid]"; return 0; }
|
||||
fi
|
||||
do_start "$@"
|
||||
}
|
||||
## 配置service
|
||||
service() {
|
||||
|
||||
do_start() {
|
||||
tempCahr='$'
|
||||
|
||||
## 创建service文件
|
||||
working_dir=$(dirname "$jarfile")
|
||||
pushd "$working_dir" > /dev/null
|
||||
mkdir -p "$PID_FOLDER" &> /dev/null
|
||||
if [[ -n "$run_user" ]]; then
|
||||
checkPermissions || return $?
|
||||
chown "$run_user" "$PID_FOLDER"
|
||||
chown "$run_user" "$pid_file"
|
||||
chown "$run_user" "$log_file"
|
||||
if [ $USE_START_STOP_DAEMON = true ] && type start-stop-daemon > /dev/null 2>&1; then
|
||||
start-stop-daemon --start --quiet \
|
||||
--chuid "$run_user" \
|
||||
--name "$identity" \
|
||||
--make-pidfile --pidfile "$pid_file" \
|
||||
--background --no-close \
|
||||
--startas "$javaexe" \
|
||||
--chdir "$working_dir" \
|
||||
-- "${arguments[@]}" \
|
||||
>> "$log_file" 2>&1
|
||||
await_file "$pid_file"
|
||||
else
|
||||
su -s /bin/sh -c "$javaexe $(printf "\"%s\" " "${arguments[@]}") >> \"$log_file\" 2>&1 & echo \$!" "$run_user" > "$pid_file"
|
||||
fi
|
||||
pid=$(cat "$pid_file")
|
||||
else
|
||||
checkPermissions || return $?
|
||||
"$javaexe" "${arguments[@]}" >> "$log_file" 2>&1 &
|
||||
pid=$!
|
||||
disown $pid
|
||||
echo "$pid" > "$pid_file"
|
||||
fi
|
||||
[[ -z $pid ]] && { echoRed "Failed to start"; return 1; }
|
||||
echoGreen "Started [$pid]"
|
||||
service_file="/etc/systemd/system/$1.service"
|
||||
|
||||
echo "[Unit]" > $service_file
|
||||
echo "Description=$1" >> $service_file
|
||||
echo "After=rc-local.service network.target remote-fs.target nss-lookup.target" >> $service_file
|
||||
|
||||
echo "" >> $service_file
|
||||
echo "[Service]" >> $service_file
|
||||
echo "Type=forking" >> $service_file
|
||||
echo "WorkingDirectory=$working_dir" >> $service_file
|
||||
echo "ExecStart=/bin/sh -c '$javaexe -Xms64m -Xmx128m -Dserver.port=$2 -Dspring.profiles.active=$3 -jar $jarfile > /dev/null 2>&1 &'" >> $service_file
|
||||
echo "ExecReload=/bin/kill -s HUP ${tempCahr}MAINPID" >> $service_file
|
||||
echo "ExecStop=/bin/kill -s QUIT ${tempCahr}MAINPID" >> $service_file
|
||||
|
||||
echo "" >> $service_file
|
||||
echo "[Install]" >> $service_file
|
||||
echo "WantedBy=multi-user.target" >> $service_file
|
||||
systemctl daemon-reload
|
||||
|
||||
## 自启动
|
||||
systemctl enable $1
|
||||
|
||||
## 配置成功
|
||||
echoGreen "config success, use 'systemctl start $1 ' to start"
|
||||
|
||||
}
|
||||
|
||||
stop() {
|
||||
working_dir=$(dirname "$jarfile")
|
||||
pushd "$working_dir" > /dev/null
|
||||
[[ -f $pid_file ]] || { echoYellow "Not running (pidfile not found)"; return 0; }
|
||||
pid=$(cat "$pid_file")
|
||||
isRunning "$pid" || { echoYellow "Not running (process ${pid}). Removing stale pid file."; rm -f "$pid_file"; return 0; }
|
||||
do_stop "$pid" "$pid_file"
|
||||
}
|
||||
|
||||
do_stop() {
|
||||
kill "$1" &> /dev/null || { echoRed "Unable to kill process $1"; return 1; }
|
||||
for i in $(seq 1 $STOP_WAIT_TIME); do
|
||||
isRunning "$1" || { echoGreen "Stopped [$1]"; rm -f "$2"; return 0; }
|
||||
[[ $i -eq STOP_WAIT_TIME/2 ]] && kill "$1" &> /dev/null
|
||||
sleep 1
|
||||
done
|
||||
echoRed "Unable to kill process $1";
|
||||
return 1;
|
||||
}
|
||||
|
||||
restart() {
|
||||
stop && start
|
||||
}
|
||||
|
||||
status() {
|
||||
working_dir=$(dirname "$jarfile")
|
||||
pushd "$working_dir" > /dev/null
|
||||
[[ -f "$pid_file" ]] || { echoRed "Not running"; return 3; }
|
||||
pid=$(cat "$pid_file")
|
||||
isRunning "$pid" || { echoRed "Not running (process ${pid} not found)"; return 1; }
|
||||
echoGreen "Running [$pid]"
|
||||
return 0
|
||||
}
|
||||
|
||||
run() {
|
||||
pushd "$(dirname "$jarfile")" > /dev/null
|
||||
"$javaexe" "${arguments[@]}"
|
||||
result=$?
|
||||
popd > /dev/null
|
||||
return "$result"
|
||||
}
|
||||
|
||||
config() {
|
||||
|
||||
## 设置自启动
|
||||
ln -s $jarfile /etc/init.d/$1
|
||||
chkconfig --add $1
|
||||
|
||||
## 生成配置文件
|
||||
working_dir=$(dirname "$jarfile")
|
||||
conf_file="$working_dir/$configfile"
|
||||
echo "LOG_FOLDER=/dev" > $conf_file
|
||||
echo "LOG_FILENAME=null" >> $conf_file
|
||||
echo "RUN_ARGS='--server.port=$2 --spring.profiles.active=$3'" >> $conf_file
|
||||
echo "JAVA_OPTS='-Dfile.encoding=utf-8'" >> $conf_file
|
||||
}
|
||||
|
||||
# Call the appropriate action function
|
||||
case "$action" in
|
||||
start)
|
||||
start "$@"; exit $?;;
|
||||
stop)
|
||||
stop "$@"; exit $?;;
|
||||
restart)
|
||||
restart "$@"; exit $?;;
|
||||
status)
|
||||
status "$@"; exit $?;;
|
||||
run)
|
||||
run "$@"; exit $?;;
|
||||
config)
|
||||
config "$1" "$2" "$3" ; exit $?;;
|
||||
*)
|
||||
echo "Usage: $0 {start|stop|restart|status|run|config}"; exit 1;
|
||||
esac
|
||||
|
||||
service "$@"
|
||||
exit 0
|
||||
|
||||
Reference in New Issue
Block a user