Skip to content

Instantly share code, notes, and snippets.

@RichardBronosky
Created July 31, 2010 04:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichardBronosky/501741 to your computer and use it in GitHub Desktop.
Save RichardBronosky/501741 to your computer and use it in GitHub Desktop.
basic init wrappers for long running processes, hudson, django runserver, etc.
#!/usr/bin/env bash
# A basic init wrapper for hudson. Designed to be generic enough for any long running process.
# Only tested on Mac OS 10.6
usage(){
cat << EOF
${0##*/} - A basic init wrapper for hudson.
Usage:
$0 ( start | stop | status )
Acknowledgments:
Copyright (c) 2010 Richard Bronosky
Offered under the terms of the MIT License.
http://www.opensource.org/licenses/mit-license.php
Created while employed by CMGdigital
EOF
exit 1
}
CMD="java -jar ${0%/*}/hudson.war"
LOG=${0%.*}.log
LOG_LINES=6
LOG_WAIT=4
PID_FILE=$(cd ${0%/*}; echo -n $(pwd)/$(basename ${0%.*}.pid))
test -f $PID_FILE && PID=$(cat $PID_FILE)
case $1 in
start)
LOG_LENGTH=$(wc -l $LOG|awk '{print $1}')
nohup $CMD >>$LOG 2>&1 &
PID=$!;
echo $PID > $PID_FILE;
if([[ $LOG_LINES > 0 ]]); then
sleep $LOG_WAIT;
awk -v lines=$LOG_LENGTH -v max=$((LOG_LENGTH+LOG_LINES)) 'NR>lines{print} NR==max{exit 0}' $LOG;
fi
;;
stop)
#kill $PID
kill $(ps -o 'ppid pid' | awk -v pid=$PID '$1==pid{print $2} END{print pid}')
while true; do
if ps $PID >/dev/null; then
sleep .1;
echo -n .;
else
break;
fi
done
echo;
[[ -f $PID_FILE ]] && rm $PID_FILE;
;;
status)
if [[ $PID > 0 ]]; then
if ps $PID | grep -q "$CMD"; then
echo "Running as pid $PID."
else
echo "Not running, even though $(cd $(dirname $PID_FILE); echo -n $(pwd)/$(basename $PID_FILE)) suggests pid $PID."
fi
else
echo "Not running."
fi
;;
*)
usage;;
esac
#!/usr/bin/env bash
# A basic init wrapper for django runserver. Designed to be generic enough for any long running process.
# Only tested on Mac OS 10.6
usage(){
cat << EOF
${0##*/} - A basic init wrapper for django runserver.
Usage:
$0 ( start | stop | status )
Acknowledgments:
Copyright (c) 2010 Richard Bronosky
Offered under the terms of the MIT License.
http://www.opensource.org/licenses/mit-license.php
Created while employed by CMGdigital
EOF
exit 1
}
CMD="django-admin.py runserver 8000"
LOG=$VIRTUAL_ENV/var/runserver.log
LOG_LINES=6
LOG_WAIT=4
PID_FILE=$VIRTUAL_ENV/var/runserver.pid
test -f $PID_FILE && PID=$(cat $PID_FILE)
case $1 in
start)
LOG_LENGTH=$(wc -l $LOG|awk '{print $1}')
nohup $CMD >>$LOG 2>&1 &
PID=$!;
echo $PID > $PID_FILE;
if([[ $LOG_LINES > 0 ]]); then
sleep $LOG_WAIT;
awk -v lines=$LOG_LENGTH -v max=$((LOG_LENGTH+LOG_LINES)) 'NR>lines{print} NR==max{exit 0}' $LOG;
fi
;;
stop)
kill $(ps -o 'ppid pid' | awk -v pid=$PID '$1==pid{print $2} END{print pid}')
while true; do
if ps $PID >/dev/null; then
sleep .1;
echo -n .;
else
break;
fi
done
echo;
[[ -f $PID_FILE ]] && rm $PID_FILE;
;;
status)
if [[ $PID > 0 ]]; then
if ps $PID | grep -q "$CMD"; then
echo "Running as pid $PID."
else
echo "Not running, even though $(cd $(dirname $PID_FILE); echo -n $(pwd)/$(basename $PID_FILE)) suggests pid $PID."
fi
else
echo "Not running."
fi
;;
*)
usage;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment