Address problems in launch.script reported by Shellcheck 0.4.1

Closes gh-4653
pull/4783/merge
Andy Wilkinson 9 years ago
parent 3352e60631
commit 81a4763940

@ -24,24 +24,26 @@
# Initialize variables that cannot be provided by a .conf file # Initialize variables that cannot be provided by a .conf file
WORKING_DIR="$(pwd)" WORKING_DIR="$(pwd)"
# shellcheck disable=SC2153
[[ -n "$JARFILE" ]] && jarfile="$JARFILE" [[ -n "$JARFILE" ]] && jarfile="$JARFILE"
[[ -n "$APP_NAME" ]] && identity="$APP_NAME" [[ -n "$APP_NAME" ]] && identity="$APP_NAME"
# Follow symlinks to find the real jar and detect init.d script # Follow symlinks to find the real jar and detect init.d script
cd $(dirname "$0") cd "$(dirname "$0")" || exit
[[ -z "$jarfile" ]] && jarfile=$(pwd)/$(basename "$0") [[ -z "$jarfile" ]] && jarfile=$(pwd)/$(basename "$0")
while [[ -L "$jarfile" ]]; do while [[ -L "$jarfile" ]]; do
[[ "$jarfile" =~ "init.d" ]] && init_script=$(basename "$jarfile") [[ "$jarfile" =~ init\.d ]] && init_script=$(basename "$jarfile")
jarfile=$(readlink "$jarfile") jarfile=$(readlink "$jarfile")
cd $(dirname "$jarfile") cd "$(dirname "$jarfile")" || exit
jarfile=$(pwd)/$(basename "$jarfile") jarfile=$(pwd)/$(basename "$jarfile")
done done
jarfolder=$(dirname "$jarfile") jarfolder="$(dirname "$jarfile")"
cd "$WORKING_DIR" cd "$WORKING_DIR" || exit
# Source any config file # Source any config file
configfile=$(basename "${jarfile%.*}.conf") configfile="$(basename "${jarfile%.*}.conf")"
[[ -r ${jarfolder}/${configfile} ]] && source ${jarfolder}/${configfile} # shellcheck source=/dev/null
[[ -r "${jarfolder}/${configfile}" ]] && source "${jarfolder}/${configfile}"
# Initialize PID/LOG locations if they weren't provided by the config file # Initialize PID/LOG locations if they weren't provided by the config file
[[ -z "$PID_FOLDER" ]] && PID_FOLDER="/var/run" [[ -z "$PID_FOLDER" ]] && PID_FOLDER="/var/run"
@ -66,9 +68,9 @@ fi
# ANSI Colors # ANSI Colors
echoRed() { echo $'\e[0;31m'$1$'\e[0m'; } echoRed() { echo $'\e[0;31m'"$1"$'\e[0m'; }
echoGreen() { echo $'\e[0;32m'$1$'\e[0m'; } echoGreen() { echo $'\e[0;32m'"$1"$'\e[0m'; }
echoYellow() { echo $'\e[0;33m'$1$'\e[0m'; } echoYellow() { echo $'\e[0;33m'"$1"$'\e[0m'; }
# Utility functions # Utility functions
checkPermissions() { checkPermissions() {
@ -77,7 +79,7 @@ checkPermissions() {
} }
isRunning() { isRunning() {
ps -p $1 &> /dev/null ps -p "$1" &> /dev/null
} }
await_file() { await_file() {
@ -108,12 +110,13 @@ pid_file="$PID_FOLDER/${identity}.pid"
log_file="$LOG_FOLDER/$LOG_FILENAME" log_file="$LOG_FOLDER/$LOG_FILENAME"
# Determine the user to run as if we are root # Determine the user to run as if we are root
# shellcheck disable=SC2012
[[ $(id -u) == "0" ]] && run_user=$(ls -ld "$jarfile" | awk '{print $3}') [[ $(id -u) == "0" ]] && run_user=$(ls -ld "$jarfile" | awk '{print $3}')
# Find Java # Find Java
if [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]]; then if [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]]; then
javaexe="$JAVA_HOME/bin/java" javaexe="$JAVA_HOME/bin/java"
elif type -p java 2>&1> /dev/null; then elif type -p java > /dev/null 2>&1; then
javaexe=$(type -p java) javaexe=$(type -p java)
elif [[ -x "/usr/bin/java" ]]; then elif [[ -x "/usr/bin/java" ]]; then
javaexe="/usr/bin/java" javaexe="/usr/bin/java"
@ -123,15 +126,15 @@ else
fi fi
# Build actual command to execute # Build actual command to execute
command="$javaexe -Dsun.misc.URLClassPath.disableJarChecking=true $JAVA_OPTS -jar $jarfile $RUN_ARGS $@" command="$javaexe -Dsun.misc.URLClassPath.disableJarChecking=true $JAVA_OPTS -jar $jarfile $RUN_ARGS $*"
# Action functions # Action functions
start() { start() {
if [[ -f "$pid_file" ]]; then if [[ -f "$pid_file" ]]; then
pid=$(cat "$pid_file") pid=$(cat "$pid_file")
isRunning $pid && { echoYellow "Already running [$pid]"; return 0; } isRunning "$pid" && { echoYellow "Already running [$pid]"; return 0; }
fi fi
do_start do_start "$@"
} }
do_start() { do_start() {
@ -143,21 +146,21 @@ do_start() {
chown "$run_user" "$PID_FOLDER" chown "$run_user" "$PID_FOLDER"
chown "$run_user" "$pid_file" chown "$run_user" "$pid_file"
chown "$run_user" "$log_file" chown "$run_user" "$log_file"
if [ {{useStartStopDaemon:true}} = true ] && which start-stop-daemon >/dev/null; then if [ "${useStartStopDaemon:-true}" = true ] && which start-stop-daemon >/dev/null; then
start-stop-daemon --start --quiet \ start-stop-daemon --start --quiet \
--chuid $run_user \ --chuid "$run_user" \
--name $identity \ --name "$identity" \
--make-pidfile --pidfile $pid_file \ --make-pidfile --pidfile "$pid_file" \
--background --no-close \ --background --no-close \
--startas $javaexe \ --startas "$javaexe" \
--chdir "$working_dir" \ --chdir "$working_dir" \
-- \ -- \
-Dsun.misc.URLClassPath.disableJarChecking=true $JAVA_OPTS \ -Dsun.misc.URLClassPath.disableJarChecking=true "${JAVA_OPTS[@]}" \
-jar $jarfile $RUN_ARGS "$@" \ -jar "$jarfile" "${RUN_ARGS[@]}" "$@" \
> $log_file 2>&1 > "$log_file" 2>&1
await_file $pid_file await_file "$pid_file"
else else
su -s /bin/sh -c "$command &> \"$log_file\" & echo \$!" $run_user > "$pid_file" su -s /bin/sh -c "$command > \"$log_file\" 2>&1 & echo \$!" "$run_user" > "$pid_file"
fi fi
pid=$(cat "$pid_file") pid=$(cat "$pid_file")
else else
@ -174,14 +177,14 @@ do_start() {
stop() { stop() {
[[ -f $pid_file ]] || { echoYellow "Not running (pidfile not found)"; return 0; } [[ -f $pid_file ]] || { echoYellow "Not running (pidfile not found)"; return 0; }
pid=$(cat "$pid_file") pid=$(cat "$pid_file")
isRunning $pid || { echoYellow "Not running (process ${pid}). Removing stale pid file."; rm -f "$pid_file"; return 0; } isRunning "$pid" || { echoYellow "Not running (process ${pid}). Removing stale pid file."; rm -f "$pid_file"; return 0; }
do_stop $pid $pid_file do_stop "$pid" "$pid_file"
} }
do_stop() { do_stop() {
kill $1 &> /dev/null || { echoRed "Unable to kill process $1"; return 1; } kill "$1" &> /dev/null || { echoRed "Unable to kill process $1"; return 1; }
for i in $(seq 1 60); do for _ in $(seq 1 60); do
isRunning $1 || { echoGreen "Stopped [$1]"; rm -f $2; return 0; } isRunning "$1" || { echoGreen "Stopped [$1]"; rm -f "$2"; return 0; }
sleep 1 sleep 1
done done
echoRed "Unable to kill process $1"; echoRed "Unable to kill process $1";
@ -196,25 +199,25 @@ force_reload() {
[[ -f $pid_file ]] || { echoRed "Not running (pidfile not found)"; return 7; } [[ -f $pid_file ]] || { echoRed "Not running (pidfile not found)"; return 7; }
pid=$(cat "$pid_file") pid=$(cat "$pid_file")
rm -f "$pid_file" rm -f "$pid_file"
isRunning $pid || { echoRed "Not running (process ${pid} not found)"; return 7; } isRunning "$pid" || { echoRed "Not running (process ${pid} not found)"; return 7; }
do_stop $pid $pid_file do_stop "$pid" "$pid_file"
do_start do_start
} }
status() { status() {
[[ -f $pid_file ]] || { echoRed "Not running"; return 3; } [[ -f "$pid_file" ]] || { echoRed "Not running"; return 3; }
pid=$(cat "$pid_file") pid=$(cat "$pid_file")
isRunning $pid || { echoRed "Not running (process ${pid} not found)"; return 1; } isRunning "$pid" || { echoRed "Not running (process ${pid} not found)"; return 1; }
echoGreen "Running [$pid]" echoGreen "Running [$pid]"
return 0 return 0
} }
run() { run() {
pushd $(dirname "$jarfile") > /dev/null pushd "$(dirname "$jarfile")" > /dev/null
exec $command $command
result=$? result=$?
popd popd
return $result return "$result"
} }
# Call the appropriate action function # Call the appropriate action function

Loading…
Cancel
Save