Skip to content

Commit

Permalink
Update the run script
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathangreen committed Apr 23, 2024
1 parent 556bdb7 commit 491894b
Showing 1 changed file with 27 additions and 35 deletions.
62 changes: 27 additions & 35 deletions bin/run
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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 () {
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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

0 comments on commit 491894b

Please sign in to comment.