#!/bin/sh
# vim:ai ts=4 sw=4
#
# tproxywatch:
#
# Use this file to start tproxy and make sure it keeps running. The handling
# of signals probably isn't correct, anyone know how to help? I would like to
# make it so that a TERM signal sent to this shell script causes a TERM
# signal to be sent to tproxy (pid in /var/log/tproxy.pid).
#

MAILTO=root
TPROXYRUN=/usr/local/sbin/tproxyrun

exec > /var/log/tproxywatch.log 2>&1

ulimit -c 0

(
	trap "rm -f /var/run/tproxywatch.pid" 0

	while true; do
		$TPROXYRUN
		exec >> /var/log/tproxywatch.log 2>&1
		echo "`date`: tproxy died, restarting.."
		date | mail -s "tproxy died, restarting.." $MAILTO
		sleep 10
	done
) &

echo "$!" > /var/run/tproxywatch.pid

sleep 1

exit 0
