diff --git a/script/init.d/zammad b/script/init.d/zammad new file mode 100755 index 000000000..4978722e0 --- /dev/null +++ b/script/init.d/zammad @@ -0,0 +1,118 @@ +#!/bin/bash + +### BEGIN INIT INFO +# Provides: zammad +# Required-Start: $local_fs $remote_fs $network $syslog +# Required-Stop: $local_fs $remote_fs $network $syslog +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: Zammad application +# Description: Zammad application +### END INIT INFO + + +APP_ROOT="/home/zammad/zammad" +PID_PATH="$APP_ROOT/tmp/pids" +WEB_SERVER_PID="$PID_PATH/puma.pid" +SOCKET_PATH="$APP_ROOT/tmp/sockets" +SOCKET_FILE="$SOCKET_PATH/zammad.socket" + +APP_USER="zammad" +DAEMON_OPTS="-p 3000 -d -e production --pidfile $WEB_SERVER_PID" + +NAME="zammad" +DESC="Zammad application" + + +check_daemons() { + PUMA_PID=-1 + PUMA_OK=1 + if [ -f $WEB_SERVER_PID ]; then + PUMA_PID=$(pgrep -F $WEB_SERVER_PID) + PUMA_OK=$? + fi + MISC_OK=0 + MISC_PID=0 +} + +execute() { + sudo -u $APP_USER -H bash -l -c "$1" +} + +start() { + cd $APP_ROOT + check_daemons + if [ $PUMA_OK -eq 0 ]; then + echo Puma is already running + exit 1 + fi + execute "RAILS_ENV=production puma $DAEMON_OPTS" + echo "$DESC started" +} + +stop() { + cd $APP_ROOT + check_daemons + if [ "$PUMA_OK" -eq 0 ]; then + ## Program is running, stop it. + kill -QUIT $PUMA_PID + test -f "$WEB_SERVER_PID" && rm -f "$WEB_SERVER_PID" + echo "$DESC stopped" + else + ## Program is not running, exit with error. + echo "Error! $DESC is not started!" + exit 1 + fi +} + +restart() { + cd $APP_ROOT + check_daemons + if [ "$PUMA_OK" -ne 0 -a "$MISC" -ne 0 ]; then + echo "Restarting $DESC..." + kill -USR2 $PUMA_PID + echo "$DESC restarted." + else + echo "Error, $NAME not running!" + exit 1 + fi +} + +status() { + cd $APP_ROOT + check_daemons + if [ $PUMA_OK -eq 0 ]; then + echo "$DESC / Puma with PID $PUMA_PID is running." + else + echo "$DESC is not running." + exit 1 + fi +} + +## Check to see if we are running as root first. +## Found at http://www.cyberciti.biz/tips/shell-root-user-check-script.html +if [ "$(id -u)" != "0" ]; then + echo "This script must be run as root" + exit 1 +fi + +case "$1" in + start) + start + ;; + stop) + stop + ;; + restart) + restart + ;; + status) + status + ;; + *) + echo "Usage: service zammad {start|stop|restart|reload}" >&2 + exit 1 + ;; +esac + +exit 0