Created
July 26, 2011 21:10
-
-
Save zeelot/1108052 to your computer and use it in GitHub Desktop.
miniond (for long running minion tasks)
This file contains hidden or 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 | |
# | |
# This script is similar to minion but will do a few additional things: | |
# - PHP process is run in the background | |
# - PHP process is monitored and restarted if it exits for any reason | |
# - Added handlers for SUGHUP, SIGINT, and SIGTERM | |
# | |
# This is meant for long running minion tasks (like background workers). | |
# Shutting down the minion tasks is done by sending a SIGINT or SIGTERM signal | |
# to this miniond process. You can also restart the minion task by sending a | |
# SUGHUP signal to this process. It's useful to restart all your workers when | |
# deploying new code so that the workers reload their code as well. | |
# | |
# Usage: ./minion [task:name] [--option1=optval1 --option2=optval2] | |
# | |
# And so on. | |
# | |
# To get help, pass in --help | |
# | |
# # Minion general help | |
# ./minion --help | |
# ./minion | |
# | |
# # Task specific help | |
# ./minion task:name --help | |
# | |
if [[ $# > 0 && $1 != --* ]] | |
then | |
TASK="--task=$1" | |
ARGS=$@ | |
shift 1 | |
fi | |
php httpdocs/index.php --uri=minion "$TASK" $ARGS & | |
# The pid of the php task so we can monitor it | |
pid=$! | |
handle_SIGHUP() | |
{ | |
echo "Restarting" | |
kill -TERM $pid | |
# Make sure we wait until the minion task is done before starting a new one | |
wait $pid | |
# Bring the task back up | |
php httpdocs/index.php --uri=minion "$TASK" $ARGS & | |
# Store the new pid of the minion task | |
pid=$! | |
} | |
handle_SIGTERM_SIGINT() | |
{ | |
echo "Shutting Down" | |
kill -TERM $pid | |
# Wait for the task to exit and store the exit code | |
wait $pid | |
ecode=$? | |
# Send the exit code out | |
exit "$ecode" | |
} | |
trap handle_SIGHUP SIGHUP | |
trap handle_SIGTERM_SIGINT SIGTERM SIGINT | |
while : | |
do | |
# Pauses the script until $pid is dead | |
wait $pid | |
# Make sure someone didn't start it back up already | |
if ! kill -0 $pid > /dev/null 2>&1 | |
then | |
# Start the task back up | |
php httpdocs/index.php --uri=minion "$TASK" $ARGS & | |
pid=$! | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment