From 491894b2b05b46e4e1281dfc8ad3d1d46ae7f56b Mon Sep 17 00:00:00 2001 From: Jonathan Green Date: Tue, 23 Apr 2024 14:00:11 -0300 Subject: [PATCH] Update the run script --- bin/run | 62 +++++++++++++++++++++++++-------------------------------- 1 file changed, 27 insertions(+), 35 deletions(-) diff --git a/bin/run b/bin/run index 55cad705ba..fcb0594967 100755 --- a/bin/run +++ b/bin/run @@ -1,12 +1,12 @@ #!/bin/bash # -# Ensures that Library Simplified Circulation Manager scripts don't run +# Ensures that Palace Manager scripts don't run # if they've already been started elsewhere. It takes the filename of a # script as an argument, and should be used to run all Library Simplified # scripts, since it creates and checks again a particular PIDFILE. # -# This file should be placed in your Library Simplified Circulation Manager's -# /bin directory. It assumes your LIBSIMPLE_DIR is /var/www/circulation +# This file should be placed in your Palace Manager's +# /bin directory. It assumes your PALACE_DIR is /var/www/circulation # unless you set the environment variable otherwise. # Process an optional delay in starting the script. This parameter takes @@ -18,8 +18,8 @@ while getopts ":d:" opt; do case $opt in d) - DELAY=$(($RANDOM % $OPTARG)) - sleep $(($DELAY * 60)) + DELAY=$((RANDOM % OPTARG)) + sleep $((DELAY * 60)) # Fix parameter order for rest of script. shift shift @@ -28,6 +28,9 @@ while getopts ":d:" opt; do echo "Option -$OPTARG requires an argument." exit 127 ;; + *) echo "unknown arg" >&2 + exit 1 + ;; esac done @@ -41,7 +44,7 @@ if [[ -z "$SCRIPT_PATH" ]]; then fi # Grab the script name for logging purposes. -SCRIPT_NAME=$(basename $SCRIPT_PATH) +SCRIPT_NAME=$(basename "$SCRIPT_PATH") # Shift so any remaining arguments can be passed to the script itself. shift @@ -51,11 +54,11 @@ pidfile=$piddir/$SCRIPT_NAME.pid logdir=/var/log/simplified logfile=$logdir/$SCRIPT_NAME.log -# Assume this run file is the Library Simplified directory/core/bin +# Assume this run file is in bin/ # unless the Library Simplified directory has been set as an environment -# variable $LIBSIMPLE_DIR -if [[ -z "$LIBSIMPLE_DIR" ]]; then - LIBSIMPLE_DIR=$(dirname $(dirname $(dirname $0))) +# variable $PALACE_DIR +if [[ -z "$PALACE_DIR" ]]; then + PALACE_DIR=$(dirname "$0") fi create_dir () { @@ -64,10 +67,10 @@ create_dir () { if [[ ! -d $dir ]]; then if [[ $UID -ne 0 ]]; then # for non-root users - sudo mkdir -p $dir && sudo chown $USER $dir + sudo mkdir -p "$dir" && sudo chown "$USER" "$dir" else # for root - mkdir -p $dir + mkdir -p "$dir" fi fi } @@ -76,7 +79,7 @@ create_pidfile () { local pid="$1" local pidfile="$2" - echo $pid > $pidfile + echo "$pid" > "$pidfile" if [[ $? -ne 0 ]]; then echo "Could not create PID file" exit 1 @@ -88,51 +91,40 @@ create_pidfile () { create_dir $piddir # Check that the script exists. -FULL_SCRIPT_PATH=$LIBSIMPLE_DIR/bin/$SCRIPT_PATH +FULL_SCRIPT_PATH=$PALACE_DIR/bin/$SCRIPT_PATH if [[ ! -f $FULL_SCRIPT_PATH ]]; then - - # The script isn't in the main app bin. Try core. - FULL_SCRIPT_PATH=$LIBSIMPLE_DIR/core/bin/$SCRIPT_PATH - if [[ ! -f $FULL_SCRIPT_PATH ]]; then - echo "$SCRIPT_PATH wasn't found in $LIBSIMPLE_DIR/bin or $LIBSIMPLE_DIR/core/bin" - exit 1 - else - # This script is in core. Update the log- and pidfiles to reflect this. - core_prefix='core-' - new_filename="$core_prefix$SCRIPT_NAME" - pidfile="${pidfile/$SCRIPT_NAME/$new_filename}" - logfile="${logfile/$SCRIPT_NAME/$new_filename}" - fi + echo "$SCRIPT_PATH wasn't found in $PALACE_DIR/bin" + exit 1 fi # Confirm that process isn't still running && create PID file if [[ -f $pidfile ]]; then - pid=$(cat $pidfile) - ps -p $pid > /dev/null 2>&1 + pid=$(cat "$pidfile") + ps -p "$pid" > /dev/null 2>&1 if [[ $? -eq 0 ]]; then # Last recorded PID was found in running processes echo "$SCRIPT_NAME is already running" exit 1 else # Last recorded PID not running - create_pidfile $$ $pidfile + create_pidfile $$ "$pidfile" fi else # No PID file; assume script not running - create_pidfile $$ $pidfile + create_pidfile $$ "$pidfile" fi # Create a log file for this script if one doesn't exist create_dir $logdir if [[ ! -f $logfile ]]; then - touch $logfile + touch "$logfile" fi # Run the script and append its output to its log file. echo "Running $SCRIPT_NAME (PID: $$)" -source $LIBSIMPLE_DIR/env/bin/activate && \ - $FULL_SCRIPT_PATH "$@" >> $logfile 2>&1 +source "$PALACE_DIR"/env/bin/activate && \ + $FULL_SCRIPT_PATH "$@" >> "$logfile" 2>&1 # When it's done, remove the PID file. -rm $pidfile +rm "$pidfile" exit 0