2014-08-13 15:40:36 +00:00
|
|
|
#!/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
|
|
|
|
|
|
|
|
|
2016-11-13 23:40:13 +00:00
|
|
|
APP_ROOT="/opt/zammad"
|
2014-08-13 15:40:36 +00:00
|
|
|
PID_PATH="$APP_ROOT/tmp/pids"
|
|
|
|
WEB_SERVER_PID="$PID_PATH/puma.pid"
|
2014-08-13 16:28:18 +00:00
|
|
|
WEBSOCKET_SERVER_PID="$PID_PATH/websocket.pid"
|
2014-08-13 16:34:21 +00:00
|
|
|
SCHEDULER_SERVER_PID="$PID_PATH/websocket.pid"
|
2014-08-13 15:40:36 +00:00
|
|
|
|
|
|
|
APP_USER="zammad"
|
2014-08-13 16:28:18 +00:00
|
|
|
PUMA_OPTS="-p 3000 -d -e production --pidfile $WEB_SERVER_PID"
|
|
|
|
WEBSOCKET_OPTS="-d"
|
2014-08-13 16:34:21 +00:00
|
|
|
SCHEDULER_OPTS=""
|
2014-08-13 15:40:36 +00:00
|
|
|
|
|
|
|
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
|
2014-08-13 16:28:18 +00:00
|
|
|
WEBSOCKET_PID=-1
|
|
|
|
WEBSOCKET_OK=1
|
|
|
|
if [ -f $WEBSOCKET_SERVER_PID ]; then
|
|
|
|
WEBSOCKET_PID=$(pgrep -F $WEBSOCKET_SERVER_PID)
|
|
|
|
WEBSOCKET_OK=$?
|
|
|
|
fi
|
2014-08-13 16:34:21 +00:00
|
|
|
SCHEDULER_PID=1
|
|
|
|
SCHEDULER_OK=1
|
|
|
|
if [ -f $SCHEDULER_SERVER_PID ]; then
|
|
|
|
SCHEDULER_PID=$(pgrep -F $SCHEDULER_SERVER_PID)
|
|
|
|
SCHEDULER_OK=$?
|
|
|
|
fi
|
2014-08-13 15:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2014-08-13 16:28:18 +00:00
|
|
|
else
|
|
|
|
execute "RAILS_ENV=production puma $PUMA_OPTS"
|
|
|
|
fi
|
|
|
|
if [ $WEBSOCKET_OK -eq 0 ]; then
|
|
|
|
echo Websocket server is already running
|
|
|
|
else
|
|
|
|
execute "RAILS_ENV=production script/websocket-server.rb start $WEBSOCKET_OPTS"
|
2014-08-13 15:40:36 +00:00
|
|
|
fi
|
2014-08-13 16:34:21 +00:00
|
|
|
execute "RAILS_ENV=production script/scheduler.rb start $SCHEDULER_OPTS"
|
2014-08-13 15:40:36 +00:00
|
|
|
echo "$DESC started"
|
|
|
|
}
|
|
|
|
|
|
|
|
stop() {
|
|
|
|
cd $APP_ROOT
|
|
|
|
check_daemons
|
2014-08-13 16:28:18 +00:00
|
|
|
if [ "$PUMA_OK" -eq 0 -o $WEBSOCKET_OK -eq 0 ]; then
|
2014-08-13 15:40:36 +00:00
|
|
|
## Program is running, stop it.
|
2014-08-13 16:28:18 +00:00
|
|
|
if [ $PUMA_OK -eq 0 ]; then
|
|
|
|
kill -QUIT $PUMA_PID
|
|
|
|
test -f "$WEB_SERVER_PID" && rm -f "$WEB_SERVER_PID"
|
|
|
|
fi
|
|
|
|
if [ $WEBSOCKET_OK -eq 0 ]; then
|
|
|
|
execute "RAILS_ENV=production script/websocket-server.rb stop $WEBSOCKET_OPTS"
|
|
|
|
test -f "$WEBSOCKET_SERVER_PID" && rm -f "$WEBSOCKET_SERVER_PID"
|
|
|
|
fi
|
2014-08-13 16:34:21 +00:00
|
|
|
execute "RAILS_ENV=production script/scheduler.rb stop $SCHEDULER_OPTS"
|
2014-08-13 15:40:36 +00:00
|
|
|
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
|
2014-08-13 16:28:18 +00:00
|
|
|
if [ "$PUMA_OK" -eq 0 -o WEBSOCKET_OK -eq 0 ]; then
|
2014-08-13 15:40:36 +00:00
|
|
|
echo "Restarting $DESC..."
|
|
|
|
kill -USR2 $PUMA_PID
|
2014-08-13 16:28:18 +00:00
|
|
|
execute "RAILS_ENV=production script/websocket-server.rb stop $WEBSOCKET_OPTS"
|
|
|
|
execute "RAILS_ENV=production script/websocket-server.rb start $WEBSOCKET_OPTS"
|
2014-08-13 16:34:21 +00:00
|
|
|
execute "RAILS_ENV=production script/scheduler.rb restart $SCHEDULER_OPTS"
|
2014-08-13 15:40:36 +00:00
|
|
|
echo "$DESC restarted."
|
|
|
|
else
|
|
|
|
echo "Error, $NAME not running!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
status() {
|
|
|
|
cd $APP_ROOT
|
|
|
|
check_daemons
|
|
|
|
if [ $PUMA_OK -eq 0 ]; then
|
2014-08-13 16:28:18 +00:00
|
|
|
echo "$DESC / Puma (pid $PUMA_PID) is running."
|
2014-08-13 15:40:36 +00:00
|
|
|
else
|
2014-08-13 16:28:18 +00:00
|
|
|
echo "$DESC / Puma (pid $PUMA_PID) is not running."
|
|
|
|
fi
|
|
|
|
if [ $WEBSOCKET_OK -eq 0 ]; then
|
|
|
|
echo "Websocket server (pid $WEBSOCKET_PID) is running."
|
|
|
|
else
|
|
|
|
echo "Websocket server (pid $WEBSOCKET_PID) is not running."
|
2014-08-13 15:40:36 +00:00
|
|
|
fi
|
2014-08-13 16:34:21 +00:00
|
|
|
execute "RAILS_ENV=production script/scheduler.rb status $SCHEDULER_OPTS"
|
2014-08-13 15:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
## 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
|