zammad 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #!/bin/bash
  2. ### BEGIN INIT INFO
  3. # Provides: zammad
  4. # Required-Start: $local_fs $remote_fs $network $syslog
  5. # Required-Stop: $local_fs $remote_fs $network $syslog
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop: 0 1 6
  8. # Short-Description: Zammad application
  9. # Description: Zammad application
  10. ### END INIT INFO
  11. APP_ROOT="/home/zammad/zammad"
  12. PID_PATH="$APP_ROOT/tmp/pids"
  13. WEB_SERVER_PID="$PID_PATH/puma.pid"
  14. WEBSOCKET_SERVER_PID="$PID_PATH/websocket.pid"
  15. SCHEDULER_SERVER_PID="$PID_PATH/websocket.pid"
  16. APP_USER="zammad"
  17. PUMA_OPTS="-p 3000 -d -e production --pidfile $WEB_SERVER_PID"
  18. WEBSOCKET_OPTS="-d"
  19. SCHEDULER_OPTS=""
  20. NAME="zammad"
  21. DESC="Zammad application"
  22. check_daemons() {
  23. PUMA_PID=-1
  24. PUMA_OK=1
  25. if [ -f $WEB_SERVER_PID ]; then
  26. PUMA_PID=$(pgrep -F $WEB_SERVER_PID)
  27. PUMA_OK=$?
  28. fi
  29. WEBSOCKET_PID=-1
  30. WEBSOCKET_OK=1
  31. if [ -f $WEBSOCKET_SERVER_PID ]; then
  32. WEBSOCKET_PID=$(pgrep -F $WEBSOCKET_SERVER_PID)
  33. WEBSOCKET_OK=$?
  34. fi
  35. SCHEDULER_PID=1
  36. SCHEDULER_OK=1
  37. if [ -f $SCHEDULER_SERVER_PID ]; then
  38. SCHEDULER_PID=$(pgrep -F $SCHEDULER_SERVER_PID)
  39. SCHEDULER_OK=$?
  40. fi
  41. }
  42. execute() {
  43. sudo -u $APP_USER -H bash -l -c "$1"
  44. }
  45. start() {
  46. cd $APP_ROOT
  47. check_daemons
  48. if [ $PUMA_OK -eq 0 ]; then
  49. echo Puma is already running
  50. else
  51. execute "RAILS_ENV=production puma $PUMA_OPTS"
  52. fi
  53. if [ $WEBSOCKET_OK -eq 0 ]; then
  54. echo Websocket server is already running
  55. else
  56. execute "RAILS_ENV=production script/websocket-server.rb start $WEBSOCKET_OPTS"
  57. fi
  58. execute "RAILS_ENV=production script/scheduler.rb start $SCHEDULER_OPTS"
  59. echo "$DESC started"
  60. }
  61. stop() {
  62. cd $APP_ROOT
  63. check_daemons
  64. if [ "$PUMA_OK" -eq 0 -o $WEBSOCKET_OK -eq 0 ]; then
  65. ## Program is running, stop it.
  66. if [ $PUMA_OK -eq 0 ]; then
  67. kill -QUIT $PUMA_PID
  68. test -f "$WEB_SERVER_PID" && rm -f "$WEB_SERVER_PID"
  69. fi
  70. if [ $WEBSOCKET_OK -eq 0 ]; then
  71. execute "RAILS_ENV=production script/websocket-server.rb stop $WEBSOCKET_OPTS"
  72. test -f "$WEBSOCKET_SERVER_PID" && rm -f "$WEBSOCKET_SERVER_PID"
  73. fi
  74. execute "RAILS_ENV=production script/scheduler.rb stop $SCHEDULER_OPTS"
  75. echo "$DESC stopped"
  76. else
  77. ## Program is not running, exit with error.
  78. echo "Error! $DESC is not started!"
  79. exit 1
  80. fi
  81. }
  82. restart() {
  83. cd $APP_ROOT
  84. check_daemons
  85. if [ "$PUMA_OK" -eq 0 -o WEBSOCKET_OK -eq 0 ]; then
  86. echo "Restarting $DESC..."
  87. kill -USR2 $PUMA_PID
  88. execute "RAILS_ENV=production script/websocket-server.rb stop $WEBSOCKET_OPTS"
  89. execute "RAILS_ENV=production script/websocket-server.rb start $WEBSOCKET_OPTS"
  90. execute "RAILS_ENV=production script/scheduler.rb restart $SCHEDULER_OPTS"
  91. echo "$DESC restarted."
  92. else
  93. echo "Error, $NAME not running!"
  94. exit 1
  95. fi
  96. }
  97. status() {
  98. cd $APP_ROOT
  99. check_daemons
  100. if [ $PUMA_OK -eq 0 ]; then
  101. echo "$DESC / Puma (pid $PUMA_PID) is running."
  102. else
  103. echo "$DESC / Puma (pid $PUMA_PID) is not running."
  104. fi
  105. if [ $WEBSOCKET_OK -eq 0 ]; then
  106. echo "Websocket server (pid $WEBSOCKET_PID) is running."
  107. else
  108. echo "Websocket server (pid $WEBSOCKET_PID) is not running."
  109. fi
  110. execute "RAILS_ENV=production script/scheduler.rb status $SCHEDULER_OPTS"
  111. }
  112. ## Check to see if we are running as root first.
  113. ## Found at http://www.cyberciti.biz/tips/shell-root-user-check-script.html
  114. if [ "$(id -u)" != "0" ]; then
  115. echo "This script must be run as root"
  116. exit 1
  117. fi
  118. case "$1" in
  119. start)
  120. start
  121. ;;
  122. stop)
  123. stop
  124. ;;
  125. restart)
  126. restart
  127. ;;
  128. status)
  129. status
  130. ;;
  131. *)
  132. echo "Usage: service zammad {start|stop|restart|reload}" >&2
  133. exit 1
  134. ;;
  135. esac
  136. exit 0