Created
June 12, 2010 14:51
-
-
Save gaqzi/435791 to your computer and use it in GitHub Desktop.
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 | |
# requires bash 3 | |
URL=http://piratepad.net/ep/admin/auth | |
EXPECTED_CODE=200 | |
TEST_FILE=/tmp/pad-watcher | |
TIMEOUT=300 # 5 minutes | |
RESTART_COMMAND='/opt/piratepad/bin/restart.sh &' | |
LAST_UPDATED_JS='/opt/piratepad/etherpad/data/50x-error-handler/last-updated.js' | |
### | |
# Actual test | |
# | |
# If it has been longer than TIMEOUT seconds | |
# since TEST_FILE was touched RESTART_COMMAND is run. | |
function set_last_updated { | |
echo "var last_updated = `date +%s`;" > $LAST_UPDATED_JS | |
echo "var timeout = ${TIMEOUT};" >> $LAST_UPDATED_JS | |
} | |
test=`/usr/bin/curl -m 60 -s -S -I ${URL} | grep '^HTTP'` | |
if [[ "${test}" =~ "${EXPECTED_CODE}" ]] ; then | |
touch $TEST_FILE | |
set_last_updated | |
else | |
if [[ -f $TEST_FILE ]] ; then | |
NOW=`date +%s` # Seconds since epoch | |
FILE_TIMEOUT=`stat -c %Z ${TEST_FILE}` # Last changed since epoch | |
DIFF=`expr ${NOW} - ${FILE_TIMEOUT}` | |
if [[ $DIFF -gt $TIMEOUT ]] ; then | |
logger "[${0}] About to run: '${RESTART_COMMAND}'" | |
touch $TEST_FILE | |
echo "Restarting `date`" >> /tmp/piratepad-watcher.log | |
$RESTART_COMMAND | |
fi | |
else | |
touch $TEST_FILE | |
set_last_updated | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment