Created
December 21, 2011 18:30
-
-
Save aaronpeterson/1507110 to your computer and use it in GitHub Desktop.
Asynchronous CakePHP Shell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Any tutorial to use cakephp with supervisor ?