Skip to content

Instantly share code, notes, and snippets.

@aaronpeterson
Created December 21, 2011 18:30
Show Gist options
  • Save aaronpeterson/1507110 to your computer and use it in GitHub Desktop.
Save aaronpeterson/1507110 to your computer and use it in GitHub Desktop.
Asynchronous CakePHP Shell
#!/bin/bash
DIR="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
RUN_IN_BACKGROUND=0
MAX_FORKS=1
usage() {
cat << EOF
usage: $0 options
This script runs a cake shell in the background with -d and should be placed in
your [App]/Console dir under the name "launch" etc. Maybe good to execute local
worker instance.
launch [options] [shell name]
*or*
launch [options] -s [shell name]
OPTIONS:
-s Cake shell you want to run (overrides non-option argument)
-f Maximum number of forks
-d Run in background
EOF
}
if [ "$1" = "help" ]; then
usage
exit 1
fi
while getopts ":s:d" opt; do
case $opt in
s)
echo "Found -s, shell is now: $OPTARG"
CAKE_SH_NAME=$OPTARG
;;
f)
echo "Found -f, maximum fork count is now: $OPTARG"
MAX_FORKS=$OPTARG
;;
d)
echo "Found -d, running in daemon mode"
RUN_IN_BACKGROUND=1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
usage
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
usage
exit 1
;;
esac
done
shift $(($OPTIND - 1))
if [ -z $CAKE_SH_NAME ]; then
CAKE_SH_NAME=$1
fi
if [ -z $CAKE_SH_NAME ]; then
echo "No shell specified, use -s or last argument" >&2
echo "Try help"
exit 1
fi
# TODO: verify existence of cake shell
# TODO: refine process search or store actual PIDs in a file
PROC_COUNT=$(ps ax | grep $CAKE_SH_NAME | grep cake.php | grep -v grep | wc -l)
echo $PROC_COUNT found working
if [ $PROC_COUNT -ge $MAX_FORKS ]; then
echo "Exceeded maximum forks: $MAX_FORKS"
exit 1
fi
if [ $RUN_IN_BACKGROUND -eq 1 ]; then
echo Executing in background
$DIR/cake $CAKE_SH_NAME < /dev/null &> /dev/null & disown
NEW_PID=$!
echo "Created process $NEW_PID"
exit 0
fi
$DIR/cake $CAKE_SH_NAME
@aaronpeterson
Copy link
Author

In the event that anyone finds this...don't use it. Use supervisord along with your normal blocking shells and let supervisord manage the process. http://supervisord.org/

@paanblogger
Copy link

Any tutorial to use cakephp with supervisor ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment