From 129ce465b01a18a18c0a279f7469ac4569e38732 Mon Sep 17 00:00:00 2001 From: Livia Medeiros Date: Thu, 17 Nov 2022 06:04:26 +0900 Subject: [PATCH] Adjust installation and service scripts (#7407) * Fix install in directory with spaces * Use $() notation instead of legacy backticks * Fix help->start enlog message * Fix typo * Whitespace cleanup * Fix restart delay * Make get_status robust --- athena-start | 35 +++++++++++++++++------------------ function.sh | 8 ++++---- install.sh | 42 +++++++++++++++++++++--------------------- uninstall.sh | 8 ++++---- 4 files changed, 46 insertions(+), 47 deletions(-) diff --git a/athena-start b/athena-start index 8279011835..346fefdb79 100755 --- a/athena-start +++ b/athena-start @@ -20,8 +20,7 @@ print_start() { get_status(){ PIDFILE=.$1.pid if [ -e ${PIDFILE} ]; then - ISRUN=$(ps ax | grep $(cat ${PIDFILE}) | grep $1) - PSRUN=$(echo "$ISRUN" | awk '{ print $1 }') + PSRUN=$(pgrep -F ${PIDFILE}) fi #return ${PSRUN} #seems to cause an issue for some os } @@ -36,11 +35,11 @@ start_serv(){ echo "stat_serv, log is enabled" echo "My logfile=${LOGFILE}" if [ -z ${PSRUN} ]; then - if [ -e ./${FIFO} ]; then rm "$FIFO"; fi + if [ -e ./${FIFO} ]; then rm "$FIFO"; fi mkfifo "$FIFO"; tee "$LOGRUN" < "$FIFO" & "./$1" > "$FIFO" 2>&1 & PID=$! #"./$1" > >(tee "$LOGRUN") 2>&1 & PID=$! #bash only echo "$PID" > .$1.pid - echo "Server '$1' started at `date +"%m-%d-%H:%M-%S"`" | tee ${LOGFILE} + echo "Server '$1' started at $(date +"%m-%d-%H:%M-%S")" | tee ${LOGFILE} else echo "Cannot start '$1', because it is already running p${PSRUN}" | tee ${LOGFILE} fi @@ -48,7 +47,7 @@ start_serv(){ if [ -z ${PSRUN} ]; then ./$1& echo "$!" > .$1.pid - echo "Server '$1' started at `date +"%m-%d-%H:%M-%S"`" + echo "Server '$1' started at $(date +"%m-%d-%H:%M-%S")" else echo "Cannot start '$1', because it is already running p${PSRUN}" fi @@ -66,20 +65,20 @@ watch_serv(){ LOGFILE="$LOG_DIR/$i.launch.log" LOGRUN="$LOG_DIR/$i.log" FIFO=$i"_fifo" - - get_status $i + + get_status ${i} #echo "Echo id of $i is ${PSRUN}" if [ -z ${PSRUN} ]; then count=$((count+1)) #echo "fifo=$FIFO" echo "server '$i' is down" echo "server '$i' is down" >> ${LOGFILE} - echo "restarting server at time at `date +"%m-%d-%H:%M-%S"`" - echo "restarting server at time at `date +"%m-%d-%H:%M-%S"`" >> ${LOGFILE} + echo "restarting server at time at $(date +"%m-%d-%H:%M-%S")" + echo "restarting server at time at $(date +"%m-%d-%H:%M-%S")" >> ${LOGFILE} if [ -e $FIFO ]; then rm $FIFO; fi mkfifo "$FIFO"; tee "$LOGRUN" < "$FIFO" & "./$i" > "$FIFO" 2>&1 & PID=$! echo "$PID" > .$i.pid - if [ $2 ] && [ $2 -lt $count ]; then break; fi + if [ $2 ] && [ $2 -lt $count ]; then break; fi fi done sleep $1 @@ -101,11 +100,11 @@ restart(){ fi done done - $0 start + $0 start } case $1 in - 'start') + 'start') print_start check_files echo "Check complete." @@ -123,7 +122,7 @@ case $1 in done echo "rAthena was started." ;; - 'watch') + 'watch') if [ ! -d "$LOG_DIR" ]; then mkdir -p $LOG_DIR; fi if [ -z $2 ]; then Restart_count=10; else Restart_count=$2; fi if [ -z $3 ]; then Restart_sleep=3; else Restart_sleep=$3; fi @@ -134,7 +133,7 @@ case $1 in done watch_serv $Restart_count $Restart_sleep echo "Watching rAthena now." - ;; + ;; 'stop') for i in ${W_SRV} ${M_SRV} ${C_SRV} ${L_SRV} do @@ -155,7 +154,7 @@ case $1 in done ;; 'restart') - restart + restart "$@" ;; 'status') for i in ${L_SRV} ${C_SRV} ${M_SRV} ${W_SRV} @@ -181,7 +180,7 @@ case $1 in 'start') echo "syntax: 'start {--enlog}'" echo "This option will start the servers" - echo "--enlog will write all terminal output into a log/$servname.log file" + echo "--enlog will write all terminal output into a log/\$servname.log file" ;; 'stop') echo "This option will shut the servers down" @@ -205,11 +204,11 @@ case $1 in ;; 'val_runonce') echo "syntax: 'val_runonce'" - echo "This option will run valgrin with run-once to check the servers" + echo "This option will run valgrind with run-once to check the servers" ;; 'valchk') echo "syntax: 'valchk'" - echo "This option will run valgrin with the servers" + echo "This option will run valgrind with the servers" ;; *) echo "Please specify a command you would like more info on { start | stop | restart | status | watch }" diff --git a/function.sh b/function.sh index e74053807f..7668a85a1e 100755 --- a/function.sh +++ b/function.sh @@ -4,7 +4,7 @@ M_SRV=map-server W_SRV=web-server INST_PATH=/opt PKG=rathena -PKG_PATH=$INST_PATH/$PKG +PKG_PATH="${INST_PATH}/${PKG}" check_files() { for i in ${L_SRV} ${C_SRV} ${M_SRV} ${W_SRV} @@ -17,11 +17,11 @@ check_files() { } check_inst_right(){ - if [ ! -w $INST_PATH ]; then echo "You must have sudo right to use this install (write/read permission in /opt/ )" && exit; fi + if [ ! -w "${INST_PATH}" ]; then echo "You must have sudo right to use this install (write/read permission in ${INST_PATH}/ )" && exit; fi } inst_launch_workaround(){ - if [ -d $PKG_PATH ]; then - if [ $(pwd) != $PKG_PATH ]; then cd $PKG_PATH; fi + if [ -d "${PKG_PATH}" ]; then + if [ "$(pwd)" != "${PKG_PATH}" ]; then cd "${PKG_PATH}"; fi fi } diff --git a/install.sh b/install.sh index c3bfead793..9db255fe82 100755 --- a/install.sh +++ b/install.sh @@ -6,12 +6,12 @@ # NOTE: This requires GNU getopt. On Mac OS X and FreeBSD, you have to install this # separately; see below. -TEMP=`getopt -o d: -l destdir: -- "$@"` +TEMP=$(getopt -o d: -l destdir: -- "$@") if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi # Note the quotes around `$TEMP': they are essential! -eval set -- "$TEMP" +eval set -- "${TEMP}" -eval set -- "$TEMP" +eval set -- "${TEMP}" while [ $# -gt 0 ] do case "$1" in @@ -20,26 +20,26 @@ do shift done -echo "destdir = $PKG_PATH " +echo "destdir = ${PKG_PATH} " check_inst_right check_files -mkdir -p $PKG_PATH/bin/ -mkdir -p $PKG_PATH/etc/$PKG/ -mkdir -p $PKG_PATH/usr/$PKG/ -mkdir -p $PKG_PATH/var/$PKG/log +mkdir -p "${PKG_PATH}/bin/" +mkdir -p "${PKG_PATH}/etc/${PKG}/" +mkdir -p "${PKG_PATH}/usr/${PKG}/" +mkdir -p "${PKG_PATH}/var/${PKG}/log" #we copy all file into opt/ dir and treat dir like normal unix arborescence -cp -r db/ $PKG_PATH/var/$PKG/db -if [ -d log ]; then cp -r log/* $PKG_PATH/var/$PKG/log/; fi -cp -r conf/ $PKG_PATH/etc/$PKG/conf -cp -r npc/ $PKG_PATH/usr/$PKG/npc -cp athena-start $PKG_PATH/ -cp *-server* $PKG_PATH/bin/ +cp -r db/ "${PKG_PATH}/var/${PKG}/db" +if [ -d log ]; then cp -r log/* "${PKG_PATH}/var/${PKG}/log/"; fi +cp -r conf/ "${PKG_PATH}/etc/${PKG}/conf" +cp -r npc/ "${PKG_PATH}/usr/${PKG}/npc" +cp athena-start "${PKG_PATH}/" +cp *-server* "${PKG_PATH}/bin/" -ln -fs $PKG_PATH/var/$PKG/db/ $PKG_PATH/db -ln -fs $PKG_PATH/var/$PKG/log/ $PKG_PATH/log -ln -fs $PKG_PATH/etc/$PKG/conf/ $PKG_PATH/conf -ln -fs $PKG_PATH/usr/$PKG/npc/ $PKG_PATH/npc -ln -fs $PKG_PATH/athena-start /usr/bin/$PKG -for f in $(ls $PKG_PATH/bin/) ; do ln -fs $PKG_PATH/bin/$f $PKG_PATH/$f; done -echo "Installation is done. You can now control the server with '$PKG start'" +ln -fs "${PKG_PATH}/var/${PKG}/db/" "${PKG_PATH}/db" +ln -fs "${PKG_PATH}/var/${PKG}/log/" "${PKG_PATH}/log" +ln -fs "${PKG_PATH}/etc/${PKG}/conf/" "${PKG_PATH}/conf" +ln -fs "${PKG_PATH}/usr/${PKG}/npc/" "${PKG_PATH}/npc" +ln -fs "${PKG_PATH}/athena-start" "/usr/bin/${PKG}" +for f in $(ls "${PKG_PATH}/bin/") ; do ln -fs "${PKG_PATH}/bin/${f}" "${PKG_PATH}/${f}"; done +echo "Installation is done. You can now control the server with '${PKG} start'" diff --git a/uninstall.sh b/uninstall.sh index 7e1f7620ad..b170eb51fa 100755 --- a/uninstall.sh +++ b/uninstall.sh @@ -1,20 +1,20 @@ #!/bin/sh #source var/function . ./function.sh -echo "My pkg path is $PKG_PATH" +echo "My pkg path is ${PKG_PATH}" check_inst_right read -p "WARNING: This script is experimental. Press Ctrl+C to cancel or Enter to continue." readEnterKey case $1 in 'bin') echo "Starting binary cleanup" - rm -rf $PKG_PATH/bin/* + rm -rf "${PKG_PATH:?}"/bin/* echo "Binary files have been deleted" ;; 'all') echo "Starting uninstall" - rm -rf $PKG_PATH - rm -rf /usr/bin/$PKG + rm -rf "${PKG_PATH:?}" + rm -rf "/usr/bin/${PKG:?}" echo "Uninstallation has succeed" ;; *)