Last active
April 13, 2020 12:05
-
-
Save hjbotha/55bcd3c012be431748d4daff35ea22ab to your computer and use it in GitHub Desktop.
wrapper script to daemonise traefik
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 | |
set -e | |
#set -x | |
ROOT=$(dirname $0) | |
cd $ROOT | |
# export your cloudflare API key by uncommenting or placing the following lines (uncommented) in traefik.conf | |
#export CLOUDFLARE_EMAIL="[email protected]" | |
#export CLOUDFLARE_API_KEY="cloudflare_api_key" | |
# no edits below | |
[ -x traefik.conf ] && source traefik.conf | |
LIBDIR=$ROOT/lib | |
BINDIR=$ROOT/bin | |
LOGDIR=$ROOT/log | |
LOGFILE=$LOGDIR/traefik.log | |
RUNDIR=$ROOT/run | |
PIDFILE=$RUNDIR/traefik.pid | |
CONFDIR=$ROOT/conf | |
CONFFILE=$CONFDIR/traefik.toml | |
# Newer versions of Traefik use these variables | |
CF_API_EMAIL=$CLOUDFLARE_EMAIL | |
CF_API_KEY=$CLOUDFLARE_API_KEY | |
start_server() { | |
[ -f conf/acme.json ] && chmod 600 conf/acme.json | |
if [ -f $PIDFILE ]; then | |
PID=$(cat $PIDFILE) | |
kill -0 $PID 2> /dev/null && RUNNING=yes || rm $PIDFILE | |
fi | |
if [ "$RUNNING" == "yes" ]; then | |
echo traefik already running. | |
else | |
echo -n "Starting traefik... " | |
$BINDIR/traefik -c $CONFFILE 2>> log/acme.log & | |
if [ $? != 0 ]; then | |
echo Failed. | |
exit 1 | |
else | |
PID=$! | |
echo $PID > $PIDFILE | |
echo "Done. (PID $PID)" | |
fi | |
fi | |
} | |
stop_server() { | |
if [ -f $PIDFILE ]; then | |
PID=$(cat $PIDFILE) | |
echo -n "Stopping traefik... " | |
kill -0 $PID 2> /dev/null && kill -INT $PID || rm $PIDFILE | |
timeout 10 tail --pid=$PID -f /dev/null | |
if [ $? == 124 ]; then | |
echo "Failed. Forcing... " | |
kill -9 $PID | |
timeout 10 tail --pid=$PID -f /dev/null | |
if [ $? == 124 ]; then | |
echo Failed. | |
KILLRESULT=1 | |
else | |
KILLRESULT=0 | |
fi | |
else | |
KILLRESULT=0 | |
rm $PIDFILE | |
echo Done. | |
fi | |
else | |
echo traefik not running. | |
KILLRESULT=0 | |
fi | |
exit $KILLRESULT | |
} | |
main() { | |
case $1 in | |
start) start_server ;; | |
stop) stop_server ;; | |
restart) stop_server; | |
start_server;; | |
esac | |
} | |
main $* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing the script!