Skip to content

Instantly share code, notes, and snippets.

@allex
Created November 15, 2017 10:01
Show Gist options
  • Select an option

  • Save allex/dcc7d7a27f1c2ea43335d351136b3dcc to your computer and use it in GitHub Desktop.

Select an option

Save allex/dcc7d7a27f1c2ea43335d351136b3dcc to your computer and use it in GitHub Desktop.
#!/bin/sh
# Author: Allex Wang <http://fedor.io/>
# GistID: dcc7d7a27f1c2ea43335d351136b3dcc
# GistURL: https://gist.github.com/dcc7d7a27f1c2ea43335d351136b3dcc
set -eE
[ -f /etc/profile ] && . /etc/profile
[ "$EUID" != "0" ] || { echo >&2 "403"; exit 1; }
DIR="$(cd "`dirname $0`" && pwd)"
mkdir -p $DIR/logs || true
cmd="${1:-"$DIR/server/bin/server"}"
logfile=$DIR/logs/server.log
lockfile=$DIR/logs/lock.pid
if [ -s $lockfile ]; then
kill -9 `cat $lockfile` && sleep .5 || true
fi
if [ -f "$logfile" ]; then (
bak="${logfile}_$(date +%F-%H%M%S)"
find $DIR/logs/ -name "*.gz" -ctime +5 -exec rm -f {} +;
mv "$logfile" "$bak" && (gzip -f "$bak" &) || true
) fi
[ $? -eq 0 ] && (
cd $DIR
/usr/bin/nohup "$cmd" &>$logfile &
pid=$!
echo $pid >$lockfile
echo "Server running at $pid."
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment