celeryd 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #!/bin/bash -e
  2. # ============================================
  3. # celeryd - Starts the Celery worker daemon.
  4. # ============================================
  5. #
  6. # :Usage: /etc/init.d/celeryd {start|stop|force-reload|restart|try-restart|status}
  7. #
  8. # :Configuration file: /etc/default/celeryd
  9. #
  10. # To configure celeryd you probably need to tell it where to chdir.
  11. #
  12. # EXAMPLE CONFIGURATION
  13. # =====================
  14. #
  15. # this is an example configuration for a Python project:
  16. #
  17. # /etc/default/celeryd:
  18. #
  19. # # Where to chdir at start.
  20. # CELERYD_CHDIR="/opt/Myproject/"
  21. #
  22. # # Extra arguments to celeryd
  23. # CELERYD_OPTS="--time-limit=300"
  24. #
  25. # # Name of the celery config module.#
  26. # CELERY_CONFIG_MODULE="celeryconfig"
  27. #
  28. # EXAMPLE DJANGO CONFIGURATION
  29. # ============================
  30. #
  31. # # Where the Django project is.
  32. # CELERYD_CHDIR="/opt/Project/"
  33. #
  34. # # Name of the projects settings module.
  35. # export DJANGO_SETTINGS_MODULE="settings"
  36. #
  37. # # Path to celeryd
  38. # CELERYD="/opt/Project/manage.py"
  39. #
  40. # # Extra arguments to manage.py
  41. # CELERYD_OPTS="celeryd"
  42. #
  43. # AVAILABLE OPTIONS
  44. # =================
  45. #
  46. # * CELERYD_OPTS
  47. # Additional arguments to celeryd, see `celeryd --help` for a list.
  48. #
  49. # * CELERYD_CHDIR
  50. # Path to chdir at start. Default is to stay in the current directory.
  51. #
  52. # * CELERYD_PID_FILE
  53. # Full path to the pidfile. Default is /var/run/celeryd.pid.
  54. #
  55. # * CELERYD_LOG_FILE
  56. # Full path to the celeryd logfile. Default is /var/log/celeryd.log
  57. #
  58. # * CELERYD_LOG_LEVEL
  59. # Log level to use for celeryd. Default is INFO.
  60. #
  61. # * CELERYD
  62. # Path to the celeryd program. Default is `celeryd`.
  63. # You can point this to an virtualenv, or even use manage.py for django.
  64. #
  65. # * CELERYD_USER
  66. # User to run celeryd as. Default is current user.
  67. #
  68. # * CELERYD_GROUP
  69. # Group to run celeryd as. Default is current user.
  70. #
  71. # * VIRTUALENV
  72. # Full path to the virtualenv environment to activate. Default is none.
  73. ### BEGIN INIT INFO
  74. # Provides: celeryd
  75. # Required-Start: $network $local_fs $remote_fs
  76. # Required-Stop: $network $local_fs $remote_fs
  77. # Default-Start: 2 3 4 5
  78. # Default-Stop: 0 1 6
  79. # Short-Description: celery task worker daemon
  80. ### END INIT INFO
  81. set -e
  82. DEFAULT_CELERYD="/usr/bin/celeryd"
  83. # /etc/init.d/ssh: start and stop the celery task worker daemon.
  84. if test -f /etc/default/celeryd; then
  85. . /etc/default/celeryd
  86. fi
  87. CELERYD_LOG_FILE=${CELERYD_LOG_FILE:-${CELERYD_LOGFILE:-"/var/log/celeryd.log"}}
  88. CELERYD_PID_FILE=${CELERYD_PID_FILE:-${CELERYD_PIDFILE:-"/var/run/celeryd.pid"}}
  89. CELERYD_LOG_LEVEL=${CELERYD_LOG_LEVEL:-${CELERYD_LOGLEVEL:-"INFO"}}
  90. CELERYD=${CELERYD:-$DEFAULT_CELERYD}
  91. export CELERY_LOADER
  92. . /lib/lsb/init-functions
  93. CELERYD_OPTS="$CELERYD_OPTS -f $CELERYD_LOG_FILE -l $CELERYD_LOG_LEVEL"
  94. if [ -n "$2" ]; then
  95. CELERYD_OPTS="$CELERYD_OPTS $2"
  96. fi
  97. # Extra start-stop-daemon options, like user/group.
  98. if [ -n "$CELERYD_USER" ]; then
  99. DAEMON_OPTS="$DAEMON_OPTS --chuid $CELERYD_USER"
  100. fi
  101. if [ -n "$CELERYD_GROUP" ]; then
  102. DAEMON_OPTS="$DAEMON_OPTS --group $CELERYD_GROUP"
  103. fi
  104. if [ -n "$CELERYD_CHDIR" ]; then
  105. DAEMON_OPTS="$DAEMON_OPTS --chdir $CELERYD_CHDIR"
  106. fi
  107. # Are we running from init?
  108. run_by_init() {
  109. ([ "$previous" ] && [ "$runlevel" ]) || [ "$runlevel" = S ]
  110. }
  111. check_dev_null() {
  112. if [ ! -c /dev/null ]; then
  113. if [ "$1" = log_end_msg ]; then
  114. log_end_msg 1 || true
  115. fi
  116. if ! run_by_init; then
  117. log_action_msg "/dev/null is not a character device!"
  118. fi
  119. exit 1
  120. fi
  121. }
  122. export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
  123. stop_worker () {
  124. cmd="start-stop-daemon --stop \
  125. --quiet \
  126. $* \
  127. --pidfile $CELERYD_PID_FILE"
  128. if $cmd; then
  129. log_end_msg 0
  130. else
  131. log_end_msg 1
  132. fi
  133. }
  134. start_worker () {
  135. cmd="start-stop-daemon --start $DAEMON_OPTS \
  136. --quiet \
  137. --oknodo \
  138. --background \
  139. --make-pidfile \
  140. $* \
  141. --pidfile $CELERYD_PID_FILE
  142. --exec $CELERYD -- $CELERYD_OPTS"
  143. if [ -n "$VIRTUALENV" ]; then
  144. source $VIRTUALENV/bin/activate
  145. fi
  146. if $cmd; then
  147. log_end_msg 0
  148. else
  149. log_end_msg 1
  150. fi
  151. }
  152. case "$1" in
  153. start)
  154. check_dev_null
  155. log_daemon_msg "Starting celery task worker server" "celeryd"
  156. start_worker
  157. ;;
  158. stop)
  159. log_daemon_msg "Stopping celery task worker server" "celeryd"
  160. stop_worker --oknodo
  161. ;;
  162. reload|force-reload)
  163. echo "Use start+stop"
  164. ;;
  165. restart)
  166. log_daemon_msg "Restarting celery task worker server" "celeryd"
  167. stop_worker --oknodo --retry 30
  168. check_dev_null log_end_msg
  169. start_worker
  170. ;;
  171. try-restart)
  172. log_daemon_msg "Restarting celery task worker server" "celeryd"
  173. set +e
  174. stop_worker --retry 30
  175. RET="$?"
  176. set -e
  177. case $RET in
  178. 0)
  179. # old daemon stopped
  180. check_dev_null log_end_msg
  181. start_worker
  182. ;;
  183. 1)
  184. # daemon not running
  185. log_progress_msg "(not running)"
  186. log_end_msg 0
  187. ;;
  188. *)
  189. # failed to stop
  190. log_progress_msg "(failed to stop)"
  191. log_end_msg 1
  192. ;;
  193. esac
  194. ;;
  195. status)
  196. status_of_proc -p $CELERYD_PID_FILE $CELERYD celeryd && exit 0 || exit $?
  197. ;;
  198. *)
  199. log_action_msg "Usage: /etc/init.d/celeryd {start|stop|force-reload|restart|try-restart|status}"
  200. exit 1
  201. esac
  202. exit 0