celeryd 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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, copy this script and give it a different
  21. # name:
  22. # I.e., if my new application, "little-worker" needs an init, I
  23. # should just use:
  24. #
  25. # cp /etc/init.d/celeryd /etc/init.d/little-worker
  26. #
  27. # You can then configure this by manipulating /etc/default/little-worker.
  28. #
  29. VERSION=10.1
  30. echo "celery init v${VERSION}."
  31. if [ $(id -u) -ne 0 ]; then
  32. echo "Error: This program can only be used by the root user."
  33. echo " Unprivileged users must use the 'celery multi' utility, "
  34. echo " or 'celery worker --detach'."
  35. exit 1
  36. fi
  37. # Can be a runlevel symlink (e.g. S02celeryd)
  38. if [ -L "$0" ]; then
  39. SCRIPT_FILE=$(readlink "$0")
  40. else
  41. SCRIPT_FILE="$0"
  42. fi
  43. SCRIPT_NAME="$(basename "$SCRIPT_FILE")"
  44. DEFAULT_USER="celery"
  45. DEFAULT_PID_FILE="/var/run/celery/%n.pid"
  46. DEFAULT_LOG_FILE="/var/log/celery/%n%I.log"
  47. DEFAULT_LOG_LEVEL="INFO"
  48. DEFAULT_NODES="celery"
  49. DEFAULT_CELERYD="-m celery worker --detach"
  50. CELERY_DEFAULTS=${CELERY_DEFAULTS:-"/etc/default/${SCRIPT_NAME}"}
  51. # Make sure executable configuration script is owned by root
  52. _config_sanity() {
  53. local path="$1"
  54. local owner=$(ls -ld "$path" | awk '{print $3}')
  55. local iwgrp=$(ls -ld "$path" | cut -b 6)
  56. local iwoth=$(ls -ld "$path" | cut -b 9)
  57. if [ "$(id -u $owner)" != "0" ]; then
  58. echo "Error: Config script '$path' must be owned by root!"
  59. echo
  60. echo "Resolution:"
  61. echo "Review the file carefully and make sure it has not been "
  62. echo "modified with mailicious intent. When sure the "
  63. echo "script is safe to execute with superuser privileges "
  64. echo "you can change ownership of the script:"
  65. echo " $ sudo chown root '$path'"
  66. exit 1
  67. fi
  68. if [ "$iwoth" != "-" ]; then # S_IWOTH
  69. echo "Error: Config script '$path' cannot be writable by others!"
  70. echo
  71. echo "Resolution:"
  72. echo "Review the file carefully and make sure it has not been "
  73. echo "modified with malicious intent. When sure the "
  74. echo "script is safe to execute with superuser privileges "
  75. echo "you can change the scripts permissions:"
  76. echo " $ sudo chmod 640 '$path'"
  77. exit 1
  78. fi
  79. if [ "$iwgrp" != "-" ]; then # S_IWGRP
  80. echo "Error: Config script '$path' cannot be writable by group!"
  81. echo
  82. echo "Resolution:"
  83. echo "Review the file carefully and make sure it has not been "
  84. echo "modified with malicious intent. When sure the "
  85. echo "script is safe to execute with superuser privileges "
  86. echo "you can change the scripts permissions:"
  87. echo " $ sudo chmod 640 '$path'"
  88. exit 1
  89. fi
  90. }
  91. if [ -f "$CELERY_DEFAULTS" ]; then
  92. _config_sanity "$CELERY_DEFAULTS"
  93. echo "Using config script: $CELERY_DEFAULTS"
  94. . "$CELERY_DEFAULTS"
  95. fi
  96. # Sets --app argument for CELERY_BIN
  97. CELERY_APP_ARG=""
  98. if [ ! -z "$CELERY_APP" ]; then
  99. CELERY_APP_ARG="--app=$CELERY_APP"
  100. fi
  101. CELERYD_USER=${CELERYD_USER:-$DEFAULT_USER}
  102. # Set CELERY_CREATE_DIRS to always create log/pid dirs.
  103. CELERY_CREATE_DIRS=${CELERY_CREATE_DIRS:-0}
  104. CELERY_CREATE_RUNDIR=$CELERY_CREATE_DIRS
  105. CELERY_CREATE_LOGDIR=$CELERY_CREATE_DIRS
  106. if [ -z "$CELERYD_PID_FILE" ]; then
  107. CELERYD_PID_FILE="$DEFAULT_PID_FILE"
  108. CELERY_CREATE_RUNDIR=1
  109. fi
  110. if [ -z "$CELERYD_LOG_FILE" ]; then
  111. CELERYD_LOG_FILE="$DEFAULT_LOG_FILE"
  112. CELERY_CREATE_LOGDIR=1
  113. fi
  114. CELERYD_LOG_LEVEL=${CELERYD_LOG_LEVEL:-${CELERYD_LOGLEVEL:-$DEFAULT_LOG_LEVEL}}
  115. CELERY_BIN=${CELERY_BIN:-"celery"}
  116. CELERYD_MULTI=${CELERYD_MULTI:-"$CELERY_BIN multi"}
  117. CELERYD_NODES=${CELERYD_NODES:-$DEFAULT_NODES}
  118. export CELERY_LOADER
  119. if [ -n "$2" ]; then
  120. CELERYD_OPTS="$CELERYD_OPTS $2"
  121. fi
  122. CELERYD_LOG_DIR=`dirname $CELERYD_LOG_FILE`
  123. CELERYD_PID_DIR=`dirname $CELERYD_PID_FILE`
  124. # Extra start-stop-daemon options, like user/group.
  125. if [ -n "$CELERYD_CHDIR" ]; then
  126. DAEMON_OPTS="$DAEMON_OPTS --workdir=$CELERYD_CHDIR"
  127. fi
  128. check_dev_null() {
  129. if [ ! -c /dev/null ]; then
  130. echo "/dev/null is not a character device!"
  131. exit 75 # EX_TEMPFAIL
  132. fi
  133. }
  134. maybe_die() {
  135. if [ $? -ne 0 ]; then
  136. echo "Exiting: $* (errno $?)"
  137. exit 77 # EX_NOPERM
  138. fi
  139. }
  140. create_default_dir() {
  141. if [ ! -d "$1" ]; then
  142. echo "- Creating default directory: '$1'"
  143. mkdir -p "$1"
  144. maybe_die "Couldn't create directory $1"
  145. echo "- Changing permissions of '$1' to 02755"
  146. chmod 02755 "$1"
  147. maybe_die "Couldn't change permissions for $1"
  148. if [ -n "$CELERYD_USER" ]; then
  149. echo "- Changing owner of '$1' to '$CELERYD_USER'"
  150. chown "$CELERYD_USER" "$1"
  151. maybe_die "Couldn't change owner of $1"
  152. fi
  153. if [ -n "$CELERYD_GROUP" ]; then
  154. echo "- Changing group of '$1' to '$CELERYD_GROUP'"
  155. chgrp "$CELERYD_GROUP" "$1"
  156. maybe_die "Couldn't change group of $1"
  157. fi
  158. fi
  159. }
  160. check_paths() {
  161. if [ $CELERY_CREATE_LOGDIR -eq 1 ]; then
  162. create_default_dir "$CELERYD_LOG_DIR"
  163. fi
  164. if [ $CELERY_CREATE_RUNDIR -eq 1 ]; then
  165. create_default_dir "$CELERYD_PID_DIR"
  166. fi
  167. }
  168. create_paths() {
  169. create_default_dir "$CELERYD_LOG_DIR"
  170. create_default_dir "$CELERYD_PID_DIR"
  171. }
  172. export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
  173. _get_pidfiles () {
  174. # note: multi < 3.1.14 output to stderr, not stdout, hence the redirect.
  175. ${CELERYD_MULTI} expand "${CELERYD_PID_FILE}" ${CELERYD_NODES} 2>&1
  176. }
  177. _get_pids() {
  178. found_pids=0
  179. my_exitcode=0
  180. for pidfile in $(_get_pidfiles); do
  181. local pid=`cat "$pidfile"`
  182. local cleaned_pid=`echo "$pid" | sed -e 's/[^0-9]//g'`
  183. if [ -z "$pid" ] || [ "$cleaned_pid" != "$pid" ]; then
  184. echo "bad pid file ($pidfile)"
  185. one_failed=true
  186. my_exitcode=1
  187. else
  188. found_pids=1
  189. echo "$pid"
  190. fi
  191. if [ $found_pids -eq 0 ]; then
  192. echo "${SCRIPT_NAME}: All nodes down"
  193. exit $my_exitcode
  194. fi
  195. done
  196. }
  197. _chuid () {
  198. su "$CELERYD_USER" -c "$CELERYD_MULTI $*"
  199. }
  200. start_workers () {
  201. if [ ! -z "$CELERYD_ULIMIT" ]; then
  202. ulimit $CELERYD_ULIMIT
  203. fi
  204. _chuid $* start $CELERYD_NODES $DAEMON_OPTS \
  205. --pidfile="$CELERYD_PID_FILE" \
  206. --logfile="$CELERYD_LOG_FILE" \
  207. --loglevel="$CELERYD_LOG_LEVEL" \
  208. $CELERY_APP_ARG \
  209. $CELERYD_OPTS
  210. }
  211. dryrun () {
  212. (C_FAKEFORK=1 start_workers --verbose)
  213. }
  214. stop_workers () {
  215. _chuid stopwait $CELERYD_NODES --pidfile="$CELERYD_PID_FILE"
  216. }
  217. restart_workers () {
  218. _chuid restart $CELERYD_NODES $DAEMON_OPTS \
  219. --pidfile="$CELERYD_PID_FILE" \
  220. --logfile="$CELERYD_LOG_FILE" \
  221. --loglevel="$CELERYD_LOG_LEVEL" \
  222. $CELERY_APP_ARG \
  223. $CELERYD_OPTS
  224. }
  225. kill_workers() {
  226. _chuid kill $CELERYD_NODES --pidfile="$CELERYD_PID_FILE"
  227. }
  228. restart_workers_graceful () {
  229. echo "WARNING: Use with caution in production"
  230. echo "The workers will attempt to restart, but they may not be able to."
  231. local worker_pids=
  232. worker_pids=`_get_pids`
  233. [ "$one_failed" ] && exit 1
  234. for worker_pid in $worker_pids; do
  235. local failed=
  236. kill -HUP $worker_pid 2> /dev/null || failed=true
  237. if [ "$failed" ]; then
  238. echo "${SCRIPT_NAME} worker (pid $worker_pid) could not be restarted"
  239. one_failed=true
  240. else
  241. echo "${SCRIPT_NAME} worker (pid $worker_pid) received SIGHUP"
  242. fi
  243. done
  244. [ "$one_failed" ] && exit 1 || exit 0
  245. }
  246. check_status () {
  247. my_exitcode=0
  248. found_pids=0
  249. local one_failed=
  250. for pidfile in $(_get_pidfiles); do
  251. if [ ! -r $pidfile ]; then
  252. echo "${SCRIPT_NAME} down: no pidfiles found"
  253. one_failed=true
  254. break
  255. fi
  256. local node=`basename "$pidfile" .pid`
  257. local pid=`cat "$pidfile"`
  258. local cleaned_pid=`echo "$pid" | sed -e 's/[^0-9]//g'`
  259. if [ -z "$pid" ] || [ "$cleaned_pid" != "$pid" ]; then
  260. echo "bad pid file ($pidfile)"
  261. one_failed=true
  262. else
  263. local failed=
  264. kill -0 $pid 2> /dev/null || failed=true
  265. if [ "$failed" ]; then
  266. echo "${SCRIPT_NAME} (node $node) (pid $pid) is down, but pidfile exists!"
  267. one_failed=true
  268. else
  269. echo "${SCRIPT_NAME} (node $node) (pid $pid) is up..."
  270. fi
  271. fi
  272. done
  273. [ "$one_failed" ] && exit 1 || exit 0
  274. }
  275. case "$1" in
  276. start)
  277. check_dev_null
  278. check_paths
  279. start_workers
  280. ;;
  281. stop)
  282. check_dev_null
  283. check_paths
  284. stop_workers
  285. ;;
  286. reload|force-reload)
  287. echo "Use restart"
  288. ;;
  289. status)
  290. check_status
  291. ;;
  292. restart)
  293. check_dev_null
  294. check_paths
  295. restart_workers
  296. ;;
  297. graceful)
  298. check_dev_null
  299. restart_workers_graceful
  300. ;;
  301. kill)
  302. check_dev_null
  303. kill_workers
  304. ;;
  305. dryrun)
  306. check_dev_null
  307. dryrun
  308. ;;
  309. try-restart)
  310. check_dev_null
  311. check_paths
  312. restart_workers
  313. ;;
  314. create-paths)
  315. check_dev_null
  316. create_paths
  317. ;;
  318. check-paths)
  319. check_dev_null
  320. check_paths
  321. ;;
  322. *)
  323. echo "Usage: /etc/init.d/${SCRIPT_NAME} {start|stop|restart|graceful|kill|dryrun|create-paths}"
  324. exit 64 # EX_USAGE
  325. ;;
  326. esac
  327. exit 0