celeryd 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. # some commands work asyncronously, so we'll wait this many seconds
  38. SLEEP_SECONDS=5
  39. DEFAULT_PID_FILE="/var/run/celery/${SCRIPT_NAME}/%n.pid"
  40. DEFAULT_LOG_FILE="/var/log/celery/${SCRIPT_NAME}/%n.log"
  41. DEFAULT_LOG_LEVEL="INFO"
  42. DEFAULT_NODES="celery"
  43. DEFAULT_CELERYD="-m celery worker --detach"
  44. CELERY_DEFAULTS=${CELERY_DEFAULTS:-"/etc/default/${SCRIPT_NAME}"}
  45. test -f "$CELERY_DEFAULTS" && . "$CELERY_DEFAULTS"
  46. # Sets --app argument for CELERY_BIN
  47. CELERY_APP_ARG=""
  48. if [ ! -z "$CELERY_APP" ]; then
  49. CELERY_APP_ARG="--app=$CELERY_APP"
  50. fi
  51. # Set CELERY_CREATE_DIRS to always create log/pid dirs.
  52. CELERY_CREATE_DIRS=${CELERY_CREATE_DIRS:-0}
  53. CELERY_CREATE_RUNDIR=$CELERY_CREATE_DIRS
  54. CELERY_CREATE_LOGDIR=$CELERY_CREATE_DIRS
  55. if [ -z "$CELERYD_PID_FILE" ]; then
  56. CELERYD_PID_FILE="$DEFAULT_PID_FILE"
  57. CELERY_CREATE_RUNDIR=1
  58. fi
  59. if [ -z "$CELERYD_LOG_FILE" ]; then
  60. CELERYD_LOG_FILE="$DEFAULT_LOG_FILE"
  61. CELERY_CREATE_LOGDIR=1
  62. fi
  63. CELERYD_LOG_LEVEL=${CELERYD_LOG_LEVEL:-${CELERYD_LOGLEVEL:-$DEFAULT_LOG_LEVEL}}
  64. CELERY_BIN=${CELERY_BIN:-"celery"}
  65. CELERYD_MULTI=${CELERYD_MULTI:-"$CELERY_BIN multi"}
  66. CELERYD=${CELERYD:-$DEFAULT_CELERYD}
  67. CELERYD_NODES=${CELERYD_NODES:-$DEFAULT_NODES}
  68. export CELERY_LOADER
  69. if [ -n "$2" ]; then
  70. CELERYD_OPTS="$CELERYD_OPTS $2"
  71. fi
  72. CELERYD_LOG_DIR=`dirname $CELERYD_LOG_FILE`
  73. CELERYD_PID_DIR=`dirname $CELERYD_PID_FILE`
  74. # Extra start-stop-daemon options, like user/group.
  75. if [ -n "$CELERYD_USER" ]; then
  76. DAEMON_OPTS="$DAEMON_OPTS --uid=$CELERYD_USER"
  77. fi
  78. if [ -n "$CELERYD_GROUP" ]; then
  79. DAEMON_OPTS="$DAEMON_OPTS --gid=$CELERYD_GROUP"
  80. fi
  81. if [ -n "$CELERYD_CHDIR" ]; then
  82. DAEMON_OPTS="$DAEMON_OPTS --workdir=$CELERYD_CHDIR"
  83. fi
  84. check_dev_null() {
  85. if [ ! -c /dev/null ]; then
  86. echo "/dev/null is not a character device!"
  87. exit 75 # EX_TEMPFAIL
  88. fi
  89. }
  90. maybe_die() {
  91. if [ $? -ne 0 ]; then
  92. echo "Exiting: $* (errno $?)"
  93. exit 77 # EX_NOPERM
  94. fi
  95. }
  96. create_default_dir() {
  97. if [ ! -d "$1" ]; then
  98. echo "- Creating default directory: '$1'"
  99. mkdir -p "$1"
  100. maybe_die "Couldn't create directory $1"
  101. echo "- Changing permissions of '$1' to 02755"
  102. chmod 02755 "$1"
  103. maybe_die "Couldn't change permissions for $1"
  104. if [ -n "$CELERYD_USER" ]; then
  105. echo "- Changing owner of '$1' to '$CELERYD_USER'"
  106. chown "$CELERYD_USER" "$1"
  107. maybe_die "Couldn't change owner of $1"
  108. fi
  109. if [ -n "$CELERYD_GROUP" ]; then
  110. echo "- Changing group of '$1' to '$CELERYD_GROUP'"
  111. chgrp "$CELERYD_GROUP" "$1"
  112. maybe_die "Couldn't change group of $1"
  113. fi
  114. fi
  115. }
  116. check_paths() {
  117. if [ $CELERY_CREATE_LOGDIR -eq 1 ]; then
  118. create_default_dir "$CELERYD_LOG_DIR"
  119. fi
  120. if [ $CELERY_CREATE_RUNDIR -eq 1 ]; then
  121. create_default_dir "$CELERYD_PID_DIR"
  122. fi
  123. }
  124. create_paths() {
  125. create_default_dir "$CELERYD_LOG_DIR"
  126. create_default_dir "$CELERYD_PID_DIR"
  127. }
  128. export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
  129. _get_pid_files() {
  130. [ ! -d "$CELERYD_PID_DIR" ] && return
  131. echo `ls -1 "$CELERYD_PID_DIR"/*.pid 2> /dev/null`
  132. }
  133. _get_pids() {
  134. local pid_files=
  135. pid_files=`_get_pid_files`
  136. [ -z "$pid_files" ] && echo "${SCRIPT_NAME} is stopped" && exit 1
  137. for pid_file in $pid_files; do
  138. local pid=`cat "$pid_file"`
  139. local cleaned_pid=`echo "$pid" | sed -e 's/[^0-9]//g'`
  140. if [ -z "$pid" ] || [ "$cleaned_pid" != "$pid" ]; then
  141. echo "bad pid file ($pid_file)"
  142. one_failed=true
  143. else
  144. echo "$pid"
  145. fi
  146. done
  147. }
  148. _get_worker_pids() {
  149. local pids=
  150. pids=`_get_pids`
  151. local worker_pids=
  152. for pid in $pids; do
  153. worker_pids=`ps h --ppid $pid -o pid`
  154. [ "$worker_pids" ] && echo "$worker_pids" || one_failed=true
  155. done
  156. }
  157. stop_workers () {
  158. $CELERYD_MULTI stopwait $CELERYD_NODES --pidfile="$CELERYD_PID_FILE"
  159. sleep $SLEEP_SECONDS
  160. }
  161. start_workers () {
  162. $CELERYD_MULTI start $CELERYD_NODES $DAEMON_OPTS \
  163. --pidfile="$CELERYD_PID_FILE" \
  164. --logfile="$CELERYD_LOG_FILE" \
  165. --loglevel="$CELERYD_LOG_LEVEL" \
  166. --cmd="$CELERYD" \
  167. $CELERY_APP_ARG \
  168. $CELERYD_OPTS
  169. sleep $SLEEP_SECONDS
  170. }
  171. restart_workers () {
  172. $CELERYD_MULTI restart $CELERYD_NODES $DAEMON_OPTS \
  173. --pidfile="$CELERYD_PID_FILE" \
  174. --logfile="$CELERYD_LOG_FILE" \
  175. --loglevel="$CELERYD_LOG_LEVEL" \
  176. --cmd="$CELERYD" \
  177. $CELERY_APP_ARG \
  178. $CELERYD_OPTS
  179. sleep $SLEEP_SECONDS
  180. }
  181. restart_workers_graceful () {
  182. local worker_pids=
  183. worker_pids=`_get_worker_pids`
  184. [ "$one_failed" ] && exit 1
  185. for worker_pid in $worker_pids; do
  186. local failed=
  187. kill -HUP $worker_pid 2> /dev/null || failed=true
  188. if [ "$failed" ]; then
  189. echo "${SCRIPT_NAME} worker (pid $worker_pid) could not be restarted"
  190. one_failed=true
  191. else
  192. echo "${SCRIPT_NAME} worker (pid $worker_pid) received SIGHUP"
  193. fi
  194. done
  195. [ "$one_failed" ] && exit 1 || exit 0
  196. }
  197. check_status () {
  198. local pid_files=
  199. pid_files=`_get_pid_files`
  200. [ -z "$pid_files" ] && echo "${SCRIPT_NAME} not running (no pidfile)" && exit 1
  201. local one_failed=
  202. for pid_file in $pid_files; do
  203. local node=`basename "$pid_file" .pid`
  204. local pid=`cat "$pid_file"`
  205. local cleaned_pid=`echo "$pid" | sed -e 's/[^0-9]//g'`
  206. if [ -z "$pid" ] || [ "$cleaned_pid" != "$pid" ]; then
  207. echo "bad pid file ($pid_file)"
  208. else
  209. local failed=
  210. kill -0 $pid 2> /dev/null || failed=true
  211. if [ "$failed" ]; then
  212. echo "${SCRIPT_NAME} (node $node) (pid $pid) is stopped, but pid file exists!"
  213. one_failed=true
  214. else
  215. echo "${SCRIPT_NAME} (node $node) (pid $pid) is running..."
  216. fi
  217. fi
  218. done
  219. [ "$one_failed" ] && exit 1 || exit 0
  220. }
  221. case "$1" in
  222. start)
  223. check_dev_null
  224. check_paths
  225. start_workers
  226. ;;
  227. stop)
  228. check_dev_null
  229. check_paths
  230. stop_workers
  231. ;;
  232. reload|force-reload)
  233. echo "Use restart"
  234. ;;
  235. status)
  236. check_status
  237. ;;
  238. restart)
  239. check_dev_null
  240. check_paths
  241. restart_workers
  242. ;;
  243. restart-workers-graceful)
  244. check_dev_null
  245. restart_workers_graceful
  246. ;;
  247. try-restart)
  248. check_dev_null
  249. check_paths
  250. restart_workers
  251. ;;
  252. create-paths)
  253. check_dev_null
  254. create_paths
  255. ;;
  256. check-paths)
  257. check_dev_null
  258. check_paths
  259. ;;
  260. *)
  261. echo "Usage: /etc/init.d/${SCRIPT_NAME} {start|stop|restart|restart-workers-graceful|kill|create-paths}"
  262. exit 64 # EX_USAGE
  263. ;;
  264. esac
  265. exit 0