celeryd 10 KB

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