celeryd 8.3 KB

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