celeryd 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #!/bin/sh -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. # :Configuration file: /etc/default/celeryd
  8. #
  9. # See http://docs.celeryproject.org/en/latest/tutorials/daemonizing.html#generic-init-scripts
  10. ### BEGIN INIT INFO
  11. # Provides: celeryd
  12. # Required-Start: $network $local_fs $remote_fs
  13. # Required-Stop: $network $local_fs $remote_fs
  14. # Default-Start: 2 3 4 5
  15. # Default-Stop: 0 1 6
  16. # Short-Description: celery task worker daemon
  17. ### END INIT INFO
  18. #
  19. #
  20. # To implement separate init scripts, do NOT copy this script. Instead,
  21. # symlink it. I.e., if my new application, "little-worker" needs an init, I
  22. # should just use:
  23. #
  24. # ln -s /etc/init.d/celeryd /etc/init.d/little-worker
  25. #
  26. # You can then configure this by manipulating /etc/default/little-worker.
  27. #
  28. # If you want to have separate LSB headers in each script you can source this
  29. # script instead of symlinking:
  30. # # ...
  31. # ### END INIT INFO
  32. # source /etc/init.d/celeryd
  33. #
  34. # Setting `SCRIPT_NAME` here allows you to symlink/source this init script,
  35. # making it easy to run multiple processes on the system.
  36. SCRIPT_NAME="$(basename $0)"
  37. DEFAULT_PID_FILE="/var/run/celery/${SCRIPT_NAME}/%n.pid"
  38. DEFAULT_LOG_FILE="/var/log/celery/${SCRIPT_NAME}/%n.log"
  39. DEFAULT_LOG_LEVEL="INFO"
  40. DEFAULT_NODES="celery"
  41. CELERY_DEFAULTS=${CELERY_DEFAULTS:-"/etc/default/${SCRIPT_NAME}"}
  42. test -f "$CELERY_DEFAULTS" && . "$CELERY_DEFAULTS"
  43. # Sets --app argument for CELERY_BIN
  44. CELERY_APP_ARG=""
  45. if [ ! -z "$CELERY_APP" ]; then
  46. CELERY_APP_ARG="--app=$CELERY_APP"
  47. fi
  48. # Set CELERY_CREATE_DIRS to always create log/pid dirs.
  49. CELERY_CREATE_DIRS=${CELERY_CREATE_DIRS:-0}
  50. CELERY_CREATE_RUNDIR=$CELERY_CREATE_DIRS
  51. CELERY_CREATE_LOGDIR=$CELERY_CREATE_DIRS
  52. if [ -z "$CELERYD_PID_FILE" ]; then
  53. CELERYD_PID_FILE="$DEFAULT_PID_FILE"
  54. CELERY_CREATE_RUNDIR=1
  55. fi
  56. if [ -z "$CELERYD_LOG_FILE" ]; then
  57. CELERYD_LOG_FILE="$DEFAULT_LOG_FILE"
  58. CELERY_CREATE_LOGDIR=1
  59. fi
  60. CELERYD_LOG_LEVEL=${CELERYD_LOG_LEVEL:-${CELERYD_LOGLEVEL:-$DEFAULT_LOG_LEVEL}}
  61. CELERY_BIN=${CELERY_BIN:-"celery"}
  62. CELERYD_MULTI=${CELERYD_MULTI:-"$CELERY_BIN multi"}
  63. CELERYD_NODES=${CELERYD_NODES:-$DEFAULT_NODES}
  64. export CELERY_LOADER
  65. if [ -n "$2" ]; then
  66. CELERYD_OPTS="$CELERYD_OPTS $2"
  67. fi
  68. CELERYD_LOG_DIR=`dirname $CELERYD_LOG_FILE`
  69. CELERYD_PID_DIR=`dirname $CELERYD_PID_FILE`
  70. # Extra start-stop-daemon options, like user/group.
  71. if [ -n "$CELERYD_USER" ]; then
  72. DAEMON_OPTS="$DAEMON_OPTS --uid=$CELERYD_USER"
  73. fi
  74. if [ -n "$CELERYD_GROUP" ]; then
  75. DAEMON_OPTS="$DAEMON_OPTS --gid=$CELERYD_GROUP"
  76. fi
  77. if [ -n "$CELERYD_CHDIR" ]; then
  78. DAEMON_OPTS="$DAEMON_OPTS --workdir=$CELERYD_CHDIR"
  79. fi
  80. check_dev_null() {
  81. if [ ! -c /dev/null ]; then
  82. echo "/dev/null is not a character device!"
  83. exit 75 # EX_TEMPFAIL
  84. fi
  85. }
  86. maybe_die() {
  87. if [ $? -ne 0 ]; then
  88. echo "Exiting: $* (errno $?)"
  89. exit 77 # EX_NOPERM
  90. fi
  91. }
  92. create_default_dir() {
  93. if [ ! -d "$1" ]; then
  94. echo "- Creating default directory: '$1'"
  95. mkdir -p "$1"
  96. maybe_die "Couldn't create directory $1"
  97. echo "- Changing permissions of '$1' to 02755"
  98. chmod 02755 "$1"
  99. maybe_die "Couldn't change permissions for $1"
  100. if [ -n "$CELERYD_USER" ]; then
  101. echo "- Changing owner of '$1' to '$CELERYD_USER'"
  102. chown "$CELERYD_USER" "$1"
  103. maybe_die "Couldn't change owner of $1"
  104. fi
  105. if [ -n "$CELERYD_GROUP" ]; then
  106. echo "- Changing group of '$1' to '$CELERYD_GROUP'"
  107. chgrp "$CELERYD_GROUP" "$1"
  108. maybe_die "Couldn't change group of $1"
  109. fi
  110. fi
  111. }
  112. check_paths() {
  113. if [ $CELERY_CREATE_LOGDIR -eq 1 ]; then
  114. create_default_dir "$CELERYD_LOG_DIR"
  115. fi
  116. if [ $CELERY_CREATE_RUNDIR -eq 1 ]; then
  117. create_default_dir "$CELERYD_PID_DIR"
  118. fi
  119. }
  120. create_paths() {
  121. create_default_dir "$CELERYD_LOG_DIR"
  122. create_default_dir "$CELERYD_PID_DIR"
  123. }
  124. export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
  125. _get_pid_files() {
  126. [ ! -d "$CELERYD_PID_DIR" ] && return
  127. echo `ls -1 "$CELERYD_PID_DIR"/*.pid 2> /dev/null`
  128. }
  129. _get_pids() {
  130. local pid_files=
  131. pid_files=`_get_pid_files`
  132. [ -z "$pid_files" ] && echo "${SCRIPT_NAME} is stopped" && exit 1
  133. for pid_file in $pid_files; do
  134. local pid=`cat "$pid_file"`
  135. local cleaned_pid=`echo "$pid" | sed -e 's/[^0-9]//g'`
  136. if [ -z "$pid" ] || [ "$cleaned_pid" != "$pid" ]; then
  137. echo "bad pid file ($pid_file)"
  138. one_failed=true
  139. else
  140. echo "$pid"
  141. fi
  142. done
  143. }
  144. _get_worker_pids() {
  145. local pids=
  146. pids=`_get_pids`
  147. local worker_pids=
  148. for pid in $pids; do
  149. worker_pids=`ps h --ppid $pid -o pid`
  150. [ "$worker_pids" ] && echo "$worker_pids" || one_failed=true
  151. done
  152. }
  153. stop_workers () {
  154. $CELERYD_MULTI stopwait $CELERYD_NODES --pidfile="$CELERYD_PID_FILE"
  155. }
  156. start_workers () {
  157. $CELERYD_MULTI start $CELERYD_NODES $DAEMON_OPTS \
  158. --pidfile="$CELERYD_PID_FILE" \
  159. --logfile="$CELERYD_LOG_FILE" \
  160. --loglevel="$CELERYD_LOG_LEVEL" \
  161. $CELERY_APP_ARG \
  162. $CELERYD_OPTS
  163. }
  164. restart_workers () {
  165. $CELERYD_MULTI restart $CELERYD_NODES $DAEMON_OPTS \
  166. --pidfile="$CELERYD_PID_FILE" \
  167. --logfile="$CELERYD_LOG_FILE" \
  168. --loglevel="$CELERYD_LOG_LEVEL" \
  169. $CELERY_APP_ARG \
  170. $CELERYD_OPTS
  171. }
  172. restart_workers_graceful () {
  173. local worker_pids=
  174. worker_pids=`_get_worker_pids`
  175. [ "$one_failed" ] && exit 1
  176. for worker_pid in $worker_pids; do
  177. local failed=
  178. kill -HUP $worker_pid 2> /dev/null || failed=true
  179. if [ "$failed" ]; then
  180. echo "${SCRIPT_NAME} worker (pid $worker_pid) could not be restarted"
  181. one_failed=true
  182. else
  183. echo "${SCRIPT_NAME} worker (pid $worker_pid) received SIGHUP"
  184. fi
  185. done
  186. [ "$one_failed" ] && exit 1 || exit 0
  187. }
  188. check_status () {
  189. local pid_files=
  190. pid_files=`_get_pid_files`
  191. [ -z "$pid_files" ] && echo "${SCRIPT_NAME} not running (no pidfile)" && exit 1
  192. local one_failed=
  193. for pid_file in $pid_files; do
  194. local node=`basename "$pid_file" .pid`
  195. local pid=`cat "$pid_file"`
  196. local cleaned_pid=`echo "$pid" | sed -e 's/[^0-9]//g'`
  197. if [ -z "$pid" ] || [ "$cleaned_pid" != "$pid" ]; then
  198. echo "bad pid file ($pid_file)"
  199. else
  200. local failed=
  201. kill -0 $pid 2> /dev/null || failed=true
  202. if [ "$failed" ]; then
  203. echo "${SCRIPT_NAME} (node $node) (pid $pid) is stopped, but pid file exists!"
  204. one_failed=true
  205. else
  206. echo "${SCRIPT_NAME} (node $node) (pid $pid) is running..."
  207. fi
  208. fi
  209. done
  210. [ "$one_failed" ] && exit 1 || exit 0
  211. }
  212. case "$1" in
  213. start)
  214. check_dev_null
  215. check_paths
  216. start_workers
  217. ;;
  218. stop)
  219. check_dev_null
  220. check_paths
  221. stop_workers
  222. ;;
  223. reload|force-reload)
  224. echo "Use restart"
  225. ;;
  226. status)
  227. check_status
  228. ;;
  229. restart)
  230. check_dev_null
  231. check_paths
  232. restart_workers
  233. ;;
  234. restart-workers-graceful)
  235. check_dev_null
  236. restart_workers_graceful
  237. ;;
  238. try-restart)
  239. check_dev_null
  240. check_paths
  241. restart_workers
  242. ;;
  243. create-paths)
  244. check_dev_null
  245. create_paths
  246. ;;
  247. check-paths)
  248. check_dev_null
  249. check_paths
  250. ;;
  251. *)
  252. echo "Usage: /etc/init.d/${SCRIPT_NAME} {start|stop|restart|restart-workers-graceful|kill|create-paths}"
  253. exit 64 # EX_USAGE
  254. ;;
  255. esac
  256. exit 0