diff --git a/server/.gitignore b/server/.gitignore index 2d132e83..6b69bd69 100644 --- a/server/.gitignore +++ b/server/.gitignore @@ -6,3 +6,4 @@ /ctbrec-tunnel.sh /jre/ /server-local.sh +ctbrec.pid diff --git a/server/src/assembly/server-linux.sh b/server/src/assembly/server-linux.sh index 498096bd..59d2ecf8 100755 --- a/server/src/assembly/server-linux.sh +++ b/server/src/assembly/server-linux.sh @@ -1,7 +1,71 @@ #!/bin/bash -pushd $(dirname $0) +DIR="$(dirname "$0")" +PIDFILE="${DIR}/ctbrec.pid" JAVA=java -$JAVA -version -$JAVA -Xmx192m -cp ${name.final}.jar -Dctbrec.config=server.json ctbrec.recorder.server.HttpServer -popd \ No newline at end of file + +terminate_ctbrec() { + echo "Caught SIGTERM signal" + kill -15 $(cat "${PIDFILE}") +} + +start() { + trap terminate_ctbrec SIGTERM + trap terminate_ctbrec SIGINT + + USERDIR="$(pwd)" + cd "${DIR}" + + # start ctbrec + $JAVA -version + $JAVA -Xmx256m -cp "${DIR}/${name.final}.jar" -Dctbrec.config=server.json ctbrec.recorder.server.HttpServer & + + # write a pid file + echo $! > "${PIDFILE}" + + # wait for the process to terminate and delete the PID file + wait $(cat "${PIDFILE}") + rm "${PIDFILE}" + + cd "${USERDIR}" +} + +stop() { + if [ -e "${PIDFILE}" ]; then + PID=$(cat "${PIDFILE}") + echo "Sending TERM signal" + kill $PID + echo -n "Waiting for ctbrec to terminate..." + tail --pid=$PID -f /dev/null + echo "done" + if [ -e "${PIDFILE}" ]; then + rm "${PIDFILE}" + fi + else + echo "PID file not found" + fi +} + +status() { + if [ -e "${PIDFILE}" ]; then + echo "running" + else + echo "stopped" + fi +} + +case "$1" in +start) + start & + ;; +stop) + stop + ;; +status) + status + ;; +*) + echo "Usage: $0 {start|stop|status}" + exit 1 + ;; +esac \ No newline at end of file