2009-01-10 12:52:15 +00:00
|
|
|
#!/usr/bin/env bash
|
2009-01-04 11:30:38 +00:00
|
|
|
#*
|
|
|
|
#* PPSS, the Parallel Processing Shell Script
|
|
|
|
#*
|
|
|
|
#* Copyright (c) 2009, Louwrentius
|
|
|
|
#* All rights reserved.
|
|
|
|
#*
|
|
|
|
#* Redistribution and use in source and binary forms, with or without
|
|
|
|
#* modification, are permitted provided that the following conditions are met:
|
|
|
|
#* * Redistributions of source code must retain the above copyright
|
|
|
|
#* notice, this list of conditions and the following disclaimer.
|
|
|
|
#* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
#* notice, this list of conditions and the following disclaimer in the
|
|
|
|
#* documentation and/or other materials provided with the distribution.
|
|
|
|
#* * Neither the name of the <organization> nor the
|
|
|
|
#* names of its contributors may be used to endorse or promote products
|
|
|
|
#* derived from this software without specific prior written permission.
|
|
|
|
#*
|
|
|
|
#* THIS SOFTWARE IS PROVIDED BY Louwrentius ''AS IS'' AND ANY
|
|
|
|
#* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
#* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
#* DISCLAIMED. IN NO EVENT SHALL Louwrentius BE LIABLE FOR ANY
|
|
|
|
#* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
#* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
#* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
#* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
#* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
#* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
|
|
|
|
#------------------------------------------------------
|
|
|
|
# It should not be necessary to edit antyhing.
|
|
|
|
# Ofcource you can if it is necesary for your needs.
|
|
|
|
# Send a patch if your changes may benefit others.
|
|
|
|
#------------------------------------------------------
|
|
|
|
|
|
|
|
# Handling control-c for a clean shutdown.
|
|
|
|
trap 'kill_process; ' INT
|
|
|
|
|
|
|
|
# Setting some vars. Do not change.
|
|
|
|
SCRIPT_NAME="Parallel Processing Shell Script"
|
2009-01-24 23:36:18 +00:00
|
|
|
SCRIPT_VERSION="1.10"
|
2009-01-04 11:30:38 +00:00
|
|
|
|
|
|
|
RUNNING_SIGNAL="$0_is_running"
|
|
|
|
GLOBAL_LOCK="PPSS-$RANDOM-$RANDOM"
|
|
|
|
PAUSE_SIGNAL="pause.txt"
|
|
|
|
ARRAY_POINTER_FILE="array-pointer-$RANDOM-$RANDOM"
|
2009-01-24 22:24:26 +00:00
|
|
|
JOB_LOG_DIR="JOB_LOG"
|
2009-01-10 12:46:01 +00:00
|
|
|
LOGFILE="ppss-log.txt"
|
2009-01-04 11:30:38 +00:00
|
|
|
MAX_DELAY=2
|
|
|
|
PERCENT="0"
|
|
|
|
PID="$$"
|
|
|
|
LISTENER_PID=""
|
2009-01-05 10:26:10 +00:00
|
|
|
IFS_BACKUP="$IFS"
|
2009-01-04 11:30:38 +00:00
|
|
|
|
2009-01-24 22:24:26 +00:00
|
|
|
SSH_SERVER="" # Remote server or 'master'.
|
|
|
|
SSH_KEY="" # SSH key for ssh account.
|
2009-01-24 23:36:18 +00:00
|
|
|
SSH_OPTS="-o \"BatchMode yes\" \"ControlPath /tmp/master-%r@%h:%p\" -o \"ControlMaster auto\""
|
|
|
|
SSH_MASTER_PID=""
|
2009-01-24 22:24:26 +00:00
|
|
|
|
2009-01-10 12:32:40 +00:00
|
|
|
showusage () {
|
2009-01-04 11:30:38 +00:00
|
|
|
|
|
|
|
echo
|
|
|
|
echo "$SCRIPT_NAME"
|
|
|
|
echo "Version: $SCRIPT_VERSION"
|
|
|
|
echo
|
|
|
|
echo "Description: this script processess files or other items in parallel. It is designed to make"
|
|
|
|
echo "use of the multi-core CPUs. It will detect the number of available CPUs and start a thread "
|
|
|
|
echo "for each CPU core. It will also use hyperthreading if available."
|
|
|
|
echo
|
|
|
|
echo "Usage: $0 [ options ]"
|
|
|
|
echo
|
|
|
|
echo "Options are:"
|
|
|
|
echo
|
|
|
|
echo -e "\t- c [ command ] \t\t\tCommand to execute. Can be a custom script or just a plain command."
|
2009-01-11 20:29:13 +00:00
|
|
|
echo -e "\t- d [ directory ] \t\t\tDirectory containing items to be processed."
|
2009-01-04 11:30:38 +00:00
|
|
|
echo -e "\t- f [ input file ] \t\t\tFile containing items to be processed. Either -d or -f"
|
|
|
|
echo -e "\t- l [ logfile ] \t\t\tSpecifies name and location of the logfile."
|
|
|
|
echo -e "\t- p [ no of parallel processes ] \tOptional: specifies number of simultaneous processes manually."
|
|
|
|
echo -e "\t- j ( enable hyperthreading ) \t\tOptiona: Enable or disable hyperthreading. Enabled by default."
|
|
|
|
echo
|
2009-01-24 22:24:26 +00:00
|
|
|
echo "Options for distributed usage:"
|
|
|
|
echo
|
|
|
|
echo -e "\t- s [ username@server ] \t\tUsername and server domain name or IP-address of 'PPSS server'."
|
|
|
|
echo -e "\t- k [ SSH key file ] \t\tSSH key file used for connection with 'PPSS server'."
|
|
|
|
echo
|
2009-01-04 11:30:38 +00:00
|
|
|
echo -e "Example: encoding some wav files to mp3 using lame:"
|
|
|
|
echo
|
2009-01-24 19:06:11 +00:00
|
|
|
echo -e "$0 -c 'lame ' -d /path/to/wavfiles -l logfile -j (wach out for the space in -c)"
|
|
|
|
echo
|
2009-01-04 11:30:38 +00:00
|
|
|
}
|
|
|
|
|
2009-01-10 12:32:40 +00:00
|
|
|
kill_process () {
|
2009-01-04 11:30:38 +00:00
|
|
|
|
|
|
|
kill $LISTENER_PID >> /dev/null 2>&1
|
|
|
|
while true
|
|
|
|
do
|
|
|
|
JOBS=`ps ax | grep -v grep | grep ppss.sh | wc -l`
|
|
|
|
if [ "$JOBS" -gt "2" ]
|
|
|
|
then
|
|
|
|
for x in `ps ax | grep -v grep | grep ppss.sh | awk '{ print $1 }'`
|
|
|
|
do
|
|
|
|
if [ ! "$x" == "$PID" ] && [ ! "$x" == "$$" ]
|
|
|
|
then
|
|
|
|
kill -9 $x >> /dev/null 2>&1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
sleep 5
|
|
|
|
else
|
|
|
|
cleanup
|
|
|
|
echo -en "\033[1B"
|
|
|
|
log INFO "Finished."
|
|
|
|
echo ""
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
done
|
2009-01-24 23:36:18 +00:00
|
|
|
|
|
|
|
# The master SSH connection should be killed.
|
|
|
|
if [ ! -z "$SSH_MASTER_PID" ]
|
|
|
|
then
|
|
|
|
kill -9 "$SSH_MASTER_PID"
|
|
|
|
fi
|
2009-01-04 11:30:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-10 12:32:40 +00:00
|
|
|
cleanup () {
|
2009-01-04 11:30:38 +00:00
|
|
|
|
|
|
|
log DEBUG "$FUNCNAME - Cleaning up all temp files and processes."
|
|
|
|
|
|
|
|
if [ -e "$FIFO" ]
|
|
|
|
then
|
|
|
|
rm $FIFO
|
|
|
|
fi
|
|
|
|
|
2009-01-24 22:24:26 +00:00
|
|
|
if [ -e "$ARRAY_POINTER_FILE" ] && [ -z "$SSH_SERVER" ]
|
2009-01-04 11:30:38 +00:00
|
|
|
then
|
|
|
|
rm $ARRAY_POINTER_FILE
|
|
|
|
fi
|
|
|
|
|
2009-01-24 22:24:26 +00:00
|
|
|
if [ -e "$GLOBAL_LOCK" ] && [ -z "$SSH_SERVER" ]
|
2009-01-04 11:30:38 +00:00
|
|
|
then
|
|
|
|
rm -rf "$GLOBAL_LOCK"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -e "$RUNNING_SIGNAL" ]
|
|
|
|
then
|
|
|
|
rm "$RUNNING_SIGNAL"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# check if ppss is already running.
|
2009-01-10 12:32:40 +00:00
|
|
|
is_running () {
|
2009-01-04 11:30:38 +00:00
|
|
|
|
|
|
|
if [ -e "$RUNNING_SIGNAL" ]
|
|
|
|
then
|
|
|
|
echo
|
2009-01-04 13:34:49 +00:00
|
|
|
log INFO "$0 is already running (lock file exists)."
|
2009-01-04 11:30:38 +00:00
|
|
|
echo
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2009-01-10 12:46:01 +00:00
|
|
|
# If no arguments are specified, show usage.
|
2009-01-04 11:30:38 +00:00
|
|
|
if [ $# -eq 0 ]
|
|
|
|
then
|
|
|
|
showusage
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# If rubbish is givven as an argument, display usage info."
|
|
|
|
echo $1 | grep -e ^- >> /dev/null
|
|
|
|
ERROR=$?
|
|
|
|
if [ ! "$ERROR" == "0" ]
|
|
|
|
then
|
|
|
|
showusage
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Process any command-line options that are specified."
|
2009-01-24 22:24:26 +00:00
|
|
|
while getopts ":c:d:f:i:jhk:l:p:s:v" OPTIONS
|
2009-01-04 11:30:38 +00:00
|
|
|
do
|
|
|
|
case $OPTIONS in
|
|
|
|
f )
|
|
|
|
INPUT_FILE="$OPTARG"
|
|
|
|
if [ ! -e "$INPUT_FILE" ]
|
|
|
|
then
|
|
|
|
echo "ERROR: input file $INPUT_FILE not found."
|
2009-01-24 19:06:11 +00:00
|
|
|
cleanup
|
2009-01-04 11:30:38 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
d )
|
|
|
|
SRC_DIR="$OPTARG"
|
|
|
|
if [ ! -e "$SRC_DIR" ]
|
|
|
|
then
|
|
|
|
echo "ERROR: directory $SRC_DIR does not exist."
|
2009-01-24 19:06:11 +00:00
|
|
|
cleanup
|
2009-01-04 11:30:38 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
c )
|
|
|
|
COMMAND="$OPTARG"
|
|
|
|
;;
|
|
|
|
|
|
|
|
h )
|
|
|
|
showusage
|
|
|
|
exit 1;;
|
|
|
|
j )
|
|
|
|
HYPERTHREADING=yes
|
|
|
|
;;
|
|
|
|
l )
|
|
|
|
LOGFILE="$OPTARG"
|
|
|
|
;;
|
2009-01-24 22:24:26 +00:00
|
|
|
k )
|
|
|
|
SSH_KEY="-i $OPTARG"
|
|
|
|
;;
|
2009-01-04 11:30:38 +00:00
|
|
|
p )
|
|
|
|
TMP="$OPTARG"
|
|
|
|
if [ ! -z "$TMP" ]
|
|
|
|
then
|
|
|
|
MAX_NO_OF_RUNNING_JOBS="$TMP"
|
|
|
|
fi
|
|
|
|
;;
|
2009-01-24 22:24:26 +00:00
|
|
|
s )
|
|
|
|
SSH_SERVER="$OPTARG"
|
|
|
|
;;
|
2009-01-04 11:30:38 +00:00
|
|
|
|
|
|
|
v )
|
|
|
|
echo ""
|
|
|
|
echo "$SCRIPT_NAME version $SCRIPT_VERSION"
|
|
|
|
echo ""
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
* )
|
|
|
|
showusage
|
|
|
|
exit 1;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2009-01-24 22:24:26 +00:00
|
|
|
# This function makes local and remote operation transparent.
|
|
|
|
exec_cmd () {
|
|
|
|
|
|
|
|
CMD="$1"
|
2009-01-10 12:46:01 +00:00
|
|
|
|
2009-01-24 22:24:26 +00:00
|
|
|
if [ ! -z "$SSH_SERVER" ]
|
|
|
|
then
|
2009-01-24 23:36:18 +00:00
|
|
|
ssh "$SSH_OPTS" "$SSH_KEY" "$SSH_SERVER" "$CMD"
|
2009-01-24 22:24:26 +00:00
|
|
|
else
|
2009-01-24 23:36:18 +00:00
|
|
|
"$CMD"
|
2009-01-24 22:24:26 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2009-01-24 23:36:18 +00:00
|
|
|
# this function makes remote or local checking of existence of items transparent.
|
|
|
|
does_file_exist () {
|
|
|
|
|
|
|
|
FILE="$1"
|
|
|
|
exec_cmd "if [ -e \"$FILE\" ]; then return 0; else return 1; fi"
|
|
|
|
}
|
|
|
|
|
2009-01-24 22:24:26 +00:00
|
|
|
|
|
|
|
# Init all vars
|
2009-01-10 12:32:40 +00:00
|
|
|
init_vars () {
|
2009-01-04 11:30:38 +00:00
|
|
|
|
2009-01-24 19:06:11 +00:00
|
|
|
if [ -e "$LOGFILE" ]
|
|
|
|
then
|
|
|
|
rm $LOGFILE
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "$COMMAND" ]
|
|
|
|
then
|
|
|
|
echo
|
|
|
|
echo "ERROR - no command specified."
|
|
|
|
echo
|
|
|
|
showusage
|
|
|
|
cleanup
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2009-01-24 22:24:26 +00:00
|
|
|
exec_cmd "echo 0 > $ARRAY_POINTER_FILE"
|
2009-01-04 11:30:38 +00:00
|
|
|
|
|
|
|
FIFO=$(pwd)/fifo-$RANDOM-$RANDOM
|
|
|
|
|
|
|
|
if [ ! -e "$FIFO" ]
|
|
|
|
then
|
|
|
|
mkfifo -m 600 $FIFO
|
|
|
|
fi
|
|
|
|
|
|
|
|
exec 42<> $FIFO
|
|
|
|
|
|
|
|
touch $RUNNING_SIGNAL
|
|
|
|
|
|
|
|
if [ -z "$MAX_NO_OF_RUNNING_JOBS" ]
|
|
|
|
then
|
|
|
|
MAX_NO_OF_RUNNING_JOBS=`get_no_of_cpus $HYPERTHREADING`
|
|
|
|
fi
|
|
|
|
|
2009-01-04 13:34:49 +00:00
|
|
|
if [ ! -e "$JOB_LOG_DIR" ]
|
2009-01-04 11:30:38 +00:00
|
|
|
then
|
2009-01-11 19:37:44 +00:00
|
|
|
log INFO "Job log directory $JOB_lOG_DIR does not exist. Creating."
|
2009-01-04 13:34:49 +00:00
|
|
|
mkdir "$JOB_LOG_DIR"
|
2009-01-11 19:37:44 +00:00
|
|
|
else
|
2009-01-24 22:24:26 +00:00
|
|
|
log INFO "Job log directory $JOB_LOG_DIR exists, if it contains logs for items, these items will be skipiped."
|
2009-01-04 11:30:38 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2009-01-10 12:32:40 +00:00
|
|
|
expand_str () {
|
2009-01-04 11:30:38 +00:00
|
|
|
|
|
|
|
STR=$1
|
|
|
|
LENGTH=$TYPE_LENGTH
|
|
|
|
SPACE=" "
|
|
|
|
|
|
|
|
while [ "${#STR}" -lt "$LENGTH" ]
|
|
|
|
do
|
|
|
|
STR=$STR$SPACE
|
|
|
|
done
|
|
|
|
|
|
|
|
echo "$STR"
|
|
|
|
}
|
|
|
|
|
2009-01-10 12:32:40 +00:00
|
|
|
log () {
|
2009-01-04 11:30:38 +00:00
|
|
|
|
|
|
|
TYPE="$1"
|
|
|
|
MESG="$2"
|
|
|
|
TMP_LOG=""
|
|
|
|
TYPE_LENGTH=6
|
|
|
|
|
|
|
|
TYPE_EXP=`expand_str "$TYPE"`
|
|
|
|
|
|
|
|
DATE=`date +%b\ %d\ %H:%M:%S`
|
|
|
|
PREFIX="$DATE: ${TYPE_EXP:0:$TYPE_LENGTH} -"
|
|
|
|
|
|
|
|
LOG_MSG="$PREFIX $MESG"
|
|
|
|
|
|
|
|
echo -e "$LOG_MSG" >> "$LOGFILE"
|
|
|
|
|
|
|
|
if [ "$TYPE" == "INFO" ]
|
|
|
|
then
|
|
|
|
echo -e "$LOG_MSG"
|
|
|
|
fi
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-01-10 12:32:40 +00:00
|
|
|
check_status () {
|
2009-01-09 20:25:44 +00:00
|
|
|
|
|
|
|
ERROR="$1"
|
|
|
|
FUNCTION="$2"
|
|
|
|
MESSAGE="$3"
|
|
|
|
|
|
|
|
if [ ! "$ERROR" == "0" ]
|
|
|
|
then
|
|
|
|
log INFO "$FUNCTION - $MESSAGE"
|
2009-01-24 22:24:26 +00:00
|
|
|
cleanup
|
2009-01-09 20:25:44 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-01-24 22:24:26 +00:00
|
|
|
test_server () {
|
|
|
|
|
2009-01-24 23:36:18 +00:00
|
|
|
# Testing if the remote server works as expected.
|
2009-01-24 22:24:26 +00:00
|
|
|
if [ ! -z "$SSH_SERVER" ]
|
|
|
|
then
|
|
|
|
|
2009-01-24 23:36:18 +00:00
|
|
|
ssh -N -M "$SSH_OPTS" "$SSH_KEY" "$SSH_SERVER" &
|
|
|
|
SSH_MASTER_PID="$!"
|
2009-01-24 22:24:26 +00:00
|
|
|
check_status "$?" "$FUNCNAME" "Server $SSH_SERVER could not be reached."
|
2009-01-24 23:36:18 +00:00
|
|
|
exec_cmd "read" &
|
|
|
|
|
2009-01-24 22:24:26 +00:00
|
|
|
else
|
|
|
|
log DEBUG "No remote server specified, assuming stand-alone mode."
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2009-01-10 12:32:40 +00:00
|
|
|
get_no_of_cpus () {
|
2009-01-05 10:26:10 +00:00
|
|
|
|
|
|
|
# Use hyperthreading or not?
|
|
|
|
HPT=$1
|
|
|
|
NUMBER=""
|
|
|
|
|
|
|
|
if [ -z "$HPT" ]
|
|
|
|
then
|
|
|
|
HPT=no
|
|
|
|
fi
|
|
|
|
|
2009-01-10 12:32:40 +00:00
|
|
|
got_cpu_info () {
|
2009-01-09 20:25:44 +00:00
|
|
|
|
|
|
|
ERROR="$1"
|
|
|
|
check_status "$ERROR" "$FUNCNAME" "cannot determine number of cpu cores. Please specify a number of parallell processes manually with -p."
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-01-05 10:26:10 +00:00
|
|
|
if [ "$HPT" == "yes" ]
|
|
|
|
then
|
|
|
|
if [ `uname` == "Linux" ]
|
|
|
|
then
|
2009-01-11 21:03:34 +00:00
|
|
|
NUMBER=`cat /proc/cpuinfo | grep processor | wc -l`
|
2009-01-09 20:25:44 +00:00
|
|
|
got_cpu_info "$?"
|
|
|
|
|
2009-01-05 10:26:10 +00:00
|
|
|
elif [ `uname` == "Darwin" ]
|
|
|
|
then
|
|
|
|
NUMBER=`sysctl -a hw | grep -w logicalcpu | awk '{ print $2 }'`
|
2009-01-09 20:25:44 +00:00
|
|
|
got_cpu_info "$?"
|
|
|
|
elif [ `uname` == "FreeBSD" ]
|
|
|
|
then
|
|
|
|
NUMBER=`sysctl hw.ncpu | awk '{ print $2 }'`
|
|
|
|
got_cpu_info "$?"
|
2009-01-05 10:26:10 +00:00
|
|
|
else
|
|
|
|
NUMBER=`cat /proc/cpuinfo | grep processor | wc -l`
|
2009-01-09 20:25:44 +00:00
|
|
|
got_cpu_info "$?"
|
2009-01-05 10:26:10 +00:00
|
|
|
fi
|
|
|
|
elif [ "$HPT" == "no" ]
|
|
|
|
then
|
|
|
|
if [ `uname` == "Linux" ]
|
|
|
|
then
|
|
|
|
NUMBER=`cat /proc/cpuinfo | grep "cpu cores" | cut -d ":" -f 2 | uniq | sed -e s/\ //g`
|
2009-01-09 20:25:44 +00:00
|
|
|
got_cpu_info "$?"
|
2009-01-05 10:26:10 +00:00
|
|
|
elif [ `uname` == "Darwin" ]
|
|
|
|
then
|
|
|
|
NUMBER=`sysctl -a hw | grep -w physicalcpu | awk '{ print $2 }'`
|
2009-01-09 20:25:44 +00:00
|
|
|
got_cpu_info "$?"
|
|
|
|
elif [ `uname` == "FreeBSD" ]
|
|
|
|
then
|
|
|
|
NUMBER=`sysctl hw.ncpu | awk '{ print $2 }'`
|
|
|
|
got_cpu_info "$?"
|
2009-01-05 10:26:10 +00:00
|
|
|
else
|
|
|
|
NUMBER=`cat /proc/cpuinfo | grep "cpu cores" | cut -d ":" -f 2 | uniq | sed -e s/\ //g`
|
2009-01-09 20:25:44 +00:00
|
|
|
got_cpu_info "$?"
|
2009-01-05 10:26:10 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ! -z "$NUMBER" ]
|
|
|
|
then
|
|
|
|
echo "$NUMBER"
|
|
|
|
else
|
|
|
|
log INFO "$FUNCNAME ERROR - number of CPUs not obtained."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-10 12:32:40 +00:00
|
|
|
random_delay () {
|
2009-01-04 11:30:38 +00:00
|
|
|
|
|
|
|
ARGS="$1"
|
|
|
|
|
|
|
|
if [ -z "$ARGS" ]
|
|
|
|
then
|
|
|
|
log ERROR "$FUNCNAME Function random delay, no argument specified."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
NUMBER=$RANDOM
|
|
|
|
let "NUMBER %= $ARGS"
|
|
|
|
sleep "$NUMBER"
|
|
|
|
}
|
|
|
|
|
2009-01-24 22:24:26 +00:00
|
|
|
|
2009-01-10 12:32:40 +00:00
|
|
|
global_lock () {
|
2009-01-04 11:30:38 +00:00
|
|
|
|
2009-01-24 22:24:26 +00:00
|
|
|
exec_cmd "mkdir $GLOBAL_LOCK > /dev/null 2>&1"
|
|
|
|
ERROR="$?"
|
|
|
|
|
2009-01-04 11:30:38 +00:00
|
|
|
if [ ! "$ERROR" == "0" ]
|
|
|
|
then
|
|
|
|
return 1
|
|
|
|
else
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2009-01-10 12:32:40 +00:00
|
|
|
get_global_lock () {
|
2009-01-04 11:30:38 +00:00
|
|
|
|
|
|
|
while true
|
|
|
|
do
|
|
|
|
global_lock
|
|
|
|
ERROR="$?"
|
|
|
|
if [ ! "$ERROR" == "0" ]
|
|
|
|
then
|
|
|
|
random_delay $MAX_DELAY
|
|
|
|
continue
|
|
|
|
else
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2009-01-10 12:32:40 +00:00
|
|
|
release_global_lock () {
|
2009-01-04 11:30:38 +00:00
|
|
|
|
2009-01-24 22:24:26 +00:00
|
|
|
if [ ! -z "$SSH_SERVER" ]
|
2009-01-04 11:30:38 +00:00
|
|
|
then
|
2009-01-24 22:24:26 +00:00
|
|
|
ssh "$SSH_KEY" "$SSH_SERVER" rm -rf "$GLOBAL_LOCK"
|
2009-01-04 11:30:38 +00:00
|
|
|
else
|
2009-01-24 22:24:26 +00:00
|
|
|
rm -rf "$GLOBAL_LOCK"
|
2009-01-04 11:30:38 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2009-01-10 12:32:40 +00:00
|
|
|
are_jobs_running () {
|
2009-01-04 11:30:38 +00:00
|
|
|
|
|
|
|
NUMBER_OF_PROCS=`jobs | wc -l`
|
|
|
|
if [ "$NUMBER_OF_PROCS" -gt "1" ]
|
|
|
|
then
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2009-01-10 12:32:40 +00:00
|
|
|
get_all_items () {
|
2009-01-04 11:30:38 +00:00
|
|
|
|
|
|
|
count=0
|
|
|
|
|
|
|
|
if [ -z "$INPUT_FILE" ]
|
|
|
|
then
|
2009-01-24 22:24:26 +00:00
|
|
|
if [ ! -z "SSH_SERVER" ] # Are we running stand-alone or as a slave?"
|
|
|
|
then
|
|
|
|
ITEMS=`ssh "$SSH_KEY" "$SSH_SERVER" ls -1 $SRC_DIR`
|
|
|
|
check_status "$FUNCNAME" "Could not list files within remote source directory."
|
|
|
|
else
|
|
|
|
ITEMS=`ls -1 $SRC_DIR`
|
|
|
|
fi
|
2009-01-05 10:26:10 +00:00
|
|
|
IFS="
|
|
|
|
"
|
2009-01-04 11:30:38 +00:00
|
|
|
for x in $ITEMS
|
|
|
|
do
|
2009-01-05 10:26:10 +00:00
|
|
|
ARRAY[$count]="$x"
|
2009-01-04 11:30:38 +00:00
|
|
|
((count++))
|
|
|
|
done
|
2009-01-05 10:26:10 +00:00
|
|
|
IFS=$IFS_BACKUP
|
2009-01-04 11:30:38 +00:00
|
|
|
else
|
2009-01-24 22:24:26 +00:00
|
|
|
if [ ! -z "SSH_SERVER" ] # Are we running stand-alone or as a slave?"
|
|
|
|
then
|
|
|
|
scp "$SSH_KEY" "$SSH_SERVER:~/$INPUT_FILE" >> /dev/null 2>&!
|
|
|
|
check_status "$FUNCNAME" "Could not copy input file."
|
|
|
|
fi
|
|
|
|
|
2009-01-04 11:30:38 +00:00
|
|
|
exec 10<$INPUT_FILE
|
|
|
|
|
|
|
|
while read LINE <&10
|
|
|
|
do
|
|
|
|
ARRAY[$count]=$LINE
|
|
|
|
((count++))
|
|
|
|
done
|
|
|
|
|
|
|
|
fi
|
|
|
|
exec 10>&-
|
|
|
|
|
|
|
|
SIZE_OF_ARRAY="${#ARRAY[@]}"
|
|
|
|
if [ "$SIZE_OF_ARRAY" -le "0" ]
|
|
|
|
then
|
2009-01-24 19:06:11 +00:00
|
|
|
echo "ERROR: source file/dir seems to be empty."
|
|
|
|
cleanup
|
2009-01-04 11:30:38 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2009-01-10 12:32:40 +00:00
|
|
|
get_item () {
|
2009-01-04 11:30:38 +00:00
|
|
|
|
|
|
|
get_global_lock
|
|
|
|
|
|
|
|
SIZE_OF_ARRAY="${#ARRAY[@]}"
|
|
|
|
|
|
|
|
# Return error if the array is empty.
|
|
|
|
if [ "$SIZE_OF_ARRAY" -le "0" ]
|
|
|
|
then
|
|
|
|
release_global_lock
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# This variable is used to walk thtough all array items.
|
2009-01-24 22:24:26 +00:00
|
|
|
ARRAY_POINTER=`exec_cmd "cat $ARRAY_POINTER_FILE"`
|
2009-01-04 11:30:38 +00:00
|
|
|
|
|
|
|
# Gives a status update on the current progress..
|
|
|
|
PERCENT=`echo "100 * $ARRAY_POINTER / $SIZE_OF_ARRAY" | bc`
|
|
|
|
log INFO "Currently $PERCENT percent complete. Processed $ARRAY_POINTER of $SIZE_OF_ARRAY items."
|
|
|
|
echo -en "\033[1A"
|
|
|
|
|
|
|
|
# Check if all items have been processed.
|
|
|
|
if [ "$ARRAY_POINTER" -ge "$SIZE_OF_ARRAY" ]
|
|
|
|
then
|
|
|
|
release_global_lock
|
|
|
|
return 2
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Select an item.
|
|
|
|
ITEM="${ARRAY[$ARRAY_POINTER]}"
|
|
|
|
if [ -z "$ITEM" ]
|
|
|
|
then
|
|
|
|
((ARRAY_POINTER++))
|
2009-01-24 22:24:26 +00:00
|
|
|
exec_cmd "echo $ARRAY_POINTER > $ARRAY_POINTER_FILE"
|
2009-01-04 11:30:38 +00:00
|
|
|
release_global_lock
|
|
|
|
get_item
|
|
|
|
else
|
|
|
|
((ARRAY_POINTER++))
|
2009-01-24 22:24:26 +00:00
|
|
|
exec_cmd "echo $ARRAY_POINTER > $ARRAY_POINTER_FILE"
|
2009-01-04 11:30:38 +00:00
|
|
|
release_global_lock
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2009-01-10 12:32:40 +00:00
|
|
|
start_single_worker () {
|
2009-01-04 11:30:38 +00:00
|
|
|
|
|
|
|
get_item
|
|
|
|
ERROR=$?
|
|
|
|
if [ ! "$ERROR" == "0" ]
|
|
|
|
then
|
|
|
|
log DEBUG "Item empty, we are probably almost finished."
|
|
|
|
return 1
|
|
|
|
else
|
|
|
|
get_global_lock
|
|
|
|
echo "$ITEM" > $FIFO
|
|
|
|
release_global_lock
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2009-01-10 12:32:40 +00:00
|
|
|
commando () {
|
2009-01-04 11:30:38 +00:00
|
|
|
|
|
|
|
ITEM="$1"
|
|
|
|
|
2009-01-24 19:06:11 +00:00
|
|
|
if [ -z "$INPUT_FILE" ]
|
|
|
|
then
|
|
|
|
ITEM="$SRC_DIR/$ITEM"
|
|
|
|
fi
|
|
|
|
|
2009-01-11 21:57:00 +00:00
|
|
|
LOG_FILE_NAME=`echo $ITEM | sed s/^\\.//g | sed s/^\\.\\.//g | sed s/\\\///g`
|
|
|
|
ITEM_LOG_FILE="$JOB_LOG_DIR/$LOG_FILE_NAME"
|
|
|
|
|
2009-01-24 23:36:18 +00:00
|
|
|
does_file_exist "$ITEM_LOG_FILE"
|
|
|
|
if [ "$0" == "0" ]
|
2009-01-04 11:30:38 +00:00
|
|
|
then
|
2009-01-11 21:57:00 +00:00
|
|
|
log DEBUG "Skipping item $ITEM - already processed."
|
2009-01-04 11:30:38 +00:00
|
|
|
else
|
2009-01-11 21:57:00 +00:00
|
|
|
|
|
|
|
EXECME='$COMMAND"$ITEM" > "$ITEM_LOG_FILE" 2>&1'
|
2009-01-04 11:30:38 +00:00
|
|
|
eval "$EXECME"
|
|
|
|
fi
|
|
|
|
|
2009-01-24 23:36:18 +00:00
|
|
|
if [ ! -z "$SSH_SERVER" ]
|
|
|
|
then
|
|
|
|
get_global_lock
|
|
|
|
scp "$SSH_KEY" "$ITEM_LOG_FILE" "$SSH_SERVER:~/$JOB_LOG"
|
|
|
|
release_global_lock
|
|
|
|
fi
|
2009-01-24 22:24:26 +00:00
|
|
|
|
2009-01-04 11:30:38 +00:00
|
|
|
start_single_worker
|
|
|
|
return $?
|
|
|
|
}
|
|
|
|
|
2009-01-10 12:32:40 +00:00
|
|
|
listen_for_job () {
|
2009-01-04 11:30:38 +00:00
|
|
|
|
|
|
|
log INFO "Listener started."
|
|
|
|
while read event <& 42
|
|
|
|
do
|
|
|
|
commando "$event" &
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2009-01-10 12:32:40 +00:00
|
|
|
# This starts an number of parallel workers based on the # of parallel jobs allowed.
|
|
|
|
start_all_workers () {
|
2009-01-04 11:30:38 +00:00
|
|
|
|
|
|
|
log INFO "Starting $MAX_NO_OF_RUNNING_JOBS workers."
|
|
|
|
|
|
|
|
i=0
|
|
|
|
while [ "$i" -lt "$MAX_NO_OF_RUNNING_JOBS" ]
|
|
|
|
do
|
|
|
|
log DEBUG "$FUNCNAME - NO OF WORKERS is $i"
|
|
|
|
start_single_worker
|
|
|
|
((i++))
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-10 12:32:40 +00:00
|
|
|
# If this is called, the whole framework will execute.
|
|
|
|
main () {
|
2009-01-04 11:30:38 +00:00
|
|
|
|
|
|
|
is_running
|
|
|
|
init_vars
|
2009-01-24 19:06:11 +00:00
|
|
|
log DEBUG "---------------- START ---------------------"
|
|
|
|
log INFO "$SCRIPT_NAME version $SCRIPT_VERSION"
|
2009-01-24 22:24:26 +00:00
|
|
|
test_server
|
2009-01-04 11:30:38 +00:00
|
|
|
get_all_items
|
|
|
|
listen_for_job "$MAX_NO_OF_RUNNING_JOBS" &
|
|
|
|
LISTENER_PID=$!
|
|
|
|
start_all_workers
|
|
|
|
}
|
2009-01-10 12:32:40 +00:00
|
|
|
# This command starts the that sets the whole framework in motion.
|
2009-01-04 11:30:38 +00:00
|
|
|
main
|
|
|
|
while true
|
|
|
|
do
|
|
|
|
JOBS=`ps ax | grep -v grep | grep ppss.sh | wc -l`
|
|
|
|
if [ "$JOBS" -gt "3" ]
|
|
|
|
then
|
|
|
|
sleep 20
|
|
|
|
else
|
|
|
|
echo -en "\033[1B"
|
|
|
|
log INFO "There are no more running jobs, so we must be finished."
|
|
|
|
echo -en "\033[1B"
|
|
|
|
log INFO "Killing listener and remainig processes."
|
|
|
|
log INFO "Dying processes may display an error message."
|
|
|
|
kill_process
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
wait
|