2011-07-26 18:28:49 +00:00
|
|
|
#!/bin/sh
|
2011-07-11 16:56:29 +00:00
|
|
|
|
2020-06-01 20:02:44 +00:00
|
|
|
# This script ensures that ep-lite is automatically restarting after
|
|
|
|
# an error happens
|
2011-07-11 16:56:29 +00:00
|
|
|
|
2020-06-01 20:02:44 +00:00
|
|
|
# Handling Errors
|
|
|
|
# 0 silent
|
|
|
|
# 1 email
|
2011-07-11 16:56:29 +00:00
|
|
|
ERROR_HANDLING=0
|
2017-09-14 11:33:27 +00:00
|
|
|
# Your email address which should receive the error messages
|
2011-07-11 16:56:29 +00:00
|
|
|
EMAIL_ADDRESS="no-reply@example.com"
|
2019-10-20 00:09:22 +00:00
|
|
|
# Sets the minimum amount of time between the sending of error emails.
|
|
|
|
# This ensures you do not get spammed during an endless reboot loop
|
2011-07-11 16:56:29 +00:00
|
|
|
# It's the time in seconds
|
|
|
|
TIME_BETWEEN_EMAILS=600 # 10 minutes
|
|
|
|
|
|
|
|
# DON'T EDIT AFTER THIS LINE
|
|
|
|
|
2020-06-01 20:02:44 +00:00
|
|
|
pecho() { printf %s\\n "$*"; }
|
|
|
|
log() { pecho "$@"; }
|
|
|
|
error() { log "ERROR: $@" >&2; }
|
|
|
|
fatal() { error "$@"; exit 1; }
|
|
|
|
|
2011-07-11 16:56:29 +00:00
|
|
|
LAST_EMAIL_SEND=0
|
|
|
|
|
2020-06-01 20:02:44 +00:00
|
|
|
# Move to the folder where ep-lite is installed
|
|
|
|
cd "$(dirname "$0")"/..
|
2011-07-11 16:56:29 +00:00
|
|
|
|
2020-06-01 20:02:44 +00:00
|
|
|
# Check if a logfile parameter is set
|
|
|
|
LOG="$1"
|
|
|
|
[ -n "${LOG}" ] || fatal "Set a logfile as the first parameter"
|
|
|
|
shift
|
2011-07-11 16:56:29 +00:00
|
|
|
|
2020-06-01 20:02:44 +00:00
|
|
|
while true; do
|
|
|
|
# Try to touch the file if it doesn't exist
|
|
|
|
[ -f "${LOG}" ] || touch "${LOG}" || fatal "Logfile '${LOG}' is not writeable"
|
2011-07-11 16:56:29 +00:00
|
|
|
|
2020-06-01 20:02:44 +00:00
|
|
|
# Check if the file is writeable
|
|
|
|
[ -w "${LOG}" ] || fatal "Logfile '${LOG}' is not writeable"
|
2019-10-20 00:09:22 +00:00
|
|
|
|
2020-06-01 20:02:44 +00:00
|
|
|
# Start the application
|
|
|
|
bin/run.sh "$@" >>${LOG} 2>>${LOG}
|
2011-07-11 16:56:29 +00:00
|
|
|
|
2020-06-01 20:02:44 +00:00
|
|
|
TIME_FMT=$(date +%Y-%m-%dT%H:%M:%S%z)
|
2019-10-20 00:09:22 +00:00
|
|
|
|
2020-06-01 20:02:44 +00:00
|
|
|
# Send email
|
|
|
|
if [ "$ERROR_HANDLING" = 1 ]; then
|
2011-07-11 16:56:29 +00:00
|
|
|
TIME_NOW=$(date +%s)
|
|
|
|
TIME_SINCE_LAST_SEND=$(($TIME_NOW - $LAST_EMAIL_SEND))
|
2019-10-20 00:09:22 +00:00
|
|
|
|
2020-06-01 20:02:44 +00:00
|
|
|
if [ "$TIME_SINCE_LAST_SEND" -gt "$TIME_BETWEEN_EMAILS" ]; then
|
|
|
|
{
|
|
|
|
cat <<EOF
|
|
|
|
Server was restarted at: ${TIME_FMT}
|
|
|
|
The last 50 lines of the log before the server exited:
|
|
|
|
|
|
|
|
EOF
|
|
|
|
tail -n 50 "${LOG}"
|
|
|
|
} | mail -s "Etherpad restarted" "$EMAIL_ADDRESS"
|
2019-10-20 00:09:22 +00:00
|
|
|
|
2011-07-11 16:56:29 +00:00
|
|
|
LAST_EMAIL_SEND=$TIME_NOW
|
|
|
|
fi
|
|
|
|
fi
|
2019-10-20 00:09:22 +00:00
|
|
|
|
2020-06-01 20:02:44 +00:00
|
|
|
pecho "RESTART! ${TIME_FMT}" >>${LOG}
|
2019-10-20 00:09:22 +00:00
|
|
|
|
2020-06-01 20:02:44 +00:00
|
|
|
# Sleep 10 seconds before restart
|
2011-07-11 16:56:29 +00:00
|
|
|
sleep 10
|
|
|
|
done
|