Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make scheduler_cron.sh posix compatible #394

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 28 additions & 20 deletions scheduler_cron.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#!/bin/bash
#!/bin/sh

# Generate an error if any variable doesn't exist
set -o nounset

die() { echo "$@" 1>&2; exit 1; }

delete_lock() {
LOCKDIR=$1
rm -rf "${LOCKDIR}"
if [ $? -ne 0 ]; then
echo "Could not remove lock dir '${LOCKDIR}'. (Check permissions...)"; >&2
exit 1;
fi
[ $? -ne 0 ] && die "Could not remove lock dir '${LOCKDIR}'. (Check permissions...)"
}

# @see http://wiki.bash-hackers.org/howto/mutex
Expand All @@ -18,7 +17,7 @@ acquire_lock () {
PIDFILE="${LOCKDIR}/PID"

#echo "Trying to acquire lock '${LOCKDIR}'."
if mkdir "${LOCKDIR}" &>/dev/null; then
if mkdir "${LOCKDIR}" >/dev/null 2>&1; then

#echo "Successfully created '${LOCKDIR}'. Lock acquired"

Expand Down Expand Up @@ -46,7 +45,7 @@ acquire_lock () {
delete_lock "${LOCKDIR}"
# now try acquire new lock recursively...
#echo "Now acquiring new lock"
acquire_lock $LOCKDIR;
acquire_lock "${LOCKDIR}";
return
fi
fi
Expand All @@ -60,12 +59,12 @@ acquire_lock () {
fi

# check is the other process is still alive
if ! kill -0 $OTHERPID &>/dev/null; then
if ! kill -0 "${OTHERPID}" >/dev/null 2>&1; then
# lock is stale, remove it and restart
#echo "removing stale lock of nonexistant PID ${OTHERPID}" >&2
delete_lock "${LOCKDIR}"
# now try acquire new lock recursively...
acquire_lock $LOCKDIR;
acquire_lock "${LOCKDIR}";
else
# lock is valid and OTHERPID is active - exit, we're locked!
#echo "Other process is alive. Still locked"
Expand All @@ -75,25 +74,34 @@ acquire_lock () {
}

# Default Location of the php binary (if --php /path/to/custom/php/bin is not set)
PHP_BIN=$(which php || true)
PHP_BIN=$(command -v php || true)

# Location of the md5sum binary
MD5SUM_BIN=$(which md5sum || true)
if [ -z "${MD5SUM_BIN}" ]; then
echo "Could not find a binary for md5sum" 1>&2
exit 1
fi
MD5SUM_BIN=$(command -v md5sum)
[ $? -ne 0 ] && die "Could not find a binary for md5sum"

# https://stackoverflow.com/a/12145443
self=$(
self=${0}
while [ -L "${self}" ]
do
cd "${self%/*}" || exit 1
self=$(readlink "${self}")
done
cd "${self%/*}" || exit 1
echo "$(pwd -P)/${self##*/}"
)

# Absolute path to Magento installation shell scripts
DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")/shell" && pwd)
if [[ -z "${DIR}" || ! -d "${DIR}" ]]; then
DIR=$(cd "$(dirname "${self}")/shell" && pwd)
if [ -z "${DIR}" ] || [ ! -d "${DIR}" ]; then
echo "Could not resolve base shell directory" 1>&2
exit 1
fi

# The scheduler.php script
SCHEDULER="scheduler.php"
if [[ ! -e "${DIR}/${SCHEDULER}" || ! -r "${DIR}/${SCHEDULER}" ]]; then
if [ ! -e "${DIR}/${SCHEDULER}" ] || [ ! -r "${DIR}/${SCHEDULER}" ]; then
echo "Could not find scheduler.php script" 1>&2
exit 1
fi
Expand Down Expand Up @@ -159,11 +167,11 @@ fi
# This is to prevent multiple processes for the same cron parameters (And the only reason we don't call PHP directly)

# Unique identifier for this cron job run
IDENTIFIER=$(echo -n "${DIR}|${MODE}|${INCLUDE_GROUPS}|${EXCLUDE_GROUPS}|${INCLUDE_JOBS}|${EXCLUDE_JOBS}" | "${MD5SUM_BIN}" - | cut -f1 -d' ')
IDENTIFIER=$(printf %s "${DIR}|${MODE}|${INCLUDE_GROUPS}|${EXCLUDE_GROUPS}|${INCLUDE_JOBS}|${EXCLUDE_JOBS}" | "${MD5SUM_BIN}" - | cut -f1 -d' ')
acquire_lock "/tmp/magento.aoe_scheduler.${IDENTIFIER}.lock";

# Needed because PHP resolves symlinks before setting __FILE__
cd "${DIR}"
cd "${DIR}" || die "Couldn't change to dir ${DIR}"

# Build the options
OPTIONS=""
Expand Down