Skip to content

Instantly share code, notes, and snippets.

@jpic
Created May 3, 2012 13:07
Show Gist options
  • Save jpic/2585486 to your computer and use it in GitHub Desktop.
Save jpic/2585486 to your computer and use it in GitHub Desktop.
Some script i use to (re)spawn ztaskd instances
#!/bin/bash
#set -x
website_instance=${1:-$USERNAME}
declare -A processes_to_monitor
my_pidfile="/tmp/$website_instance/ztask_monitor.pid"
source /srv/$website_instance/env/bin/activate
cd /srv/$website_instance/main/
function safe_kill_from_pidfile() {
if [[ ! -f "$1" ]]; then
return
fi
local pid="$(<$1)"
if [[ -d /proc/$pid ]]; then
echo kill $pid
kill $pid
while [[ -d /proc/$pid ]]; do
sleep 1
done
echo $pid is dead
fi
}
for client in $(find /srv/$website_instance/main/clients/* -maxdepth 0 -type d); do
ztask_pidfile="/tmp/$website_instance/${client##*/}_ztask.pid"
ztask_logfile="/var/log/$website_instance/${client##*/}_ztask.log"
ztask_command="$client/manage.py ztaskd --noreload -f $ztask_logfile"
processes_to_monitor[$ztask_pidfile]="$ztask_command"
done
unset ztask_pidfile
unset ztask_logfile
unset ztask_command
safe_kill_from_pidfile $my_pidfile
echo $$ > $my_pidfile
for pidfile in ${!processes_to_monitor[@]}; do
safe_kill_from_pidfile $pidfile
${processes_to_monitor[$pidfile]} & disown && echo $! > $pidfile
done
while true; do
for pidfile in ${!processes_to_monitor[@]}; do
if [[ ! -f $pidfile || ! -d /proc/$(<$pidfile) ]]; then
echo ztaskd process of $pidfile is gone, starting it again | mail root
${processes_to_monitor[$pidfile]} & disown && echo $! > $pidfile
sleep 15
fi
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment