celeryd 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. 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%I.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. # Options to su
  107. # can be used to enable login shell (CELERYD_SU_ARGS="-l"),
  108. # or even to use start-stop-daemon instead of su.
  109. CELERYD_SU=${CELERY_SU:-"su"}
  110. CELERYD_SU_ARGS=${CELERYD_SU_ARGS:-""}
  111. CELERYD_USER=${CELERYD_USER:-$DEFAULT_USER}
  112. # Set CELERY_CREATE_DIRS to always create log/pid dirs.
  113. CELERY_CREATE_DIRS=${CELERY_CREATE_DIRS:-0}
  114. CELERY_CREATE_RUNDIR=$CELERY_CREATE_DIRS
  115. CELERY_CREATE_LOGDIR=$CELERY_CREATE_DIRS
  116. if [ -z "$CELERYD_PID_FILE" ]; then
  117. CELERYD_PID_FILE="$DEFAULT_PID_FILE"
  118. CELERY_CREATE_RUNDIR=1
  119. fi
  120. if [ -z "$CELERYD_LOG_FILE" ]; then
  121. CELERYD_LOG_FILE="$DEFAULT_LOG_FILE"
  122. CELERY_CREATE_LOGDIR=1
  123. fi
  124. CELERYD_LOG_LEVEL=${CELERYD_LOG_LEVEL:-${CELERYD_LOGLEVEL:-$DEFAULT_LOG_LEVEL}}
  125. CELERY_BIN=${CELERY_BIN:-"celery"}
  126. CELERYD_MULTI=${CELERYD_MULTI:-"$CELERY_BIN multi"}
  127. CELERYD_NODES=${CELERYD_NODES:-$DEFAULT_NODES}
  128. export CELERY_LOADER
  129. if [ -n "$2" ]; then
  130. CELERYD_OPTS="$CELERYD_OPTS $2"
  131. fi
  132. CELERYD_LOG_DIR=`dirname $CELERYD_LOG_FILE`
  133. CELERYD_PID_DIR=`dirname $CELERYD_PID_FILE`
  134. # Extra start-stop-daemon options, like user/group.
  135. if [ -n "$CELERYD_CHDIR" ]; then
  136. DAEMON_OPTS="$DAEMON_OPTS --workdir=$CELERYD_CHDIR"
  137. fi
  138. check_dev_null() {
  139. if [ ! -c /dev/null ]; then
  140. echo "/dev/null is not a character device!"
  141. exit 75 # EX_TEMPFAIL
  142. fi
  143. }
  144. maybe_die() {
  145. if [ $? -ne 0 ]; then
  146. echo "Exiting: $* (errno $?)"
  147. exit 77 # EX_NOPERM
  148. fi
  149. }
  150. create_default_dir() {
  151. if [ ! -d "$1" ]; then
  152. echo "- Creating default directory: '$1'"
  153. mkdir -p "$1"
  154. maybe_die "Couldn't create directory $1"
  155. echo "- Changing permissions of '$1' to 02755"
  156. chmod 02755 "$1"
  157. maybe_die "Couldn't change permissions for $1"
  158. if [ -n "$CELERYD_USER" ]; then
  159. echo "- Changing owner of '$1' to '$CELERYD_USER'"
  160. chown "$CELERYD_USER" "$1"
  161. maybe_die "Couldn't change owner of $1"
  162. fi
  163. if [ -n "$CELERYD_GROUP" ]; then
  164. echo "- Changing group of '$1' to '$CELERYD_GROUP'"
  165. chgrp "$CELERYD_GROUP" "$1"
  166. maybe_die "Couldn't change group of $1"
  167. fi
  168. fi
  169. }
  170. check_paths() {
  171. if [ $CELERY_CREATE_LOGDIR -eq 1 ]; then
  172. create_default_dir "$CELERYD_LOG_DIR"
  173. fi
  174. if [ $CELERY_CREATE_RUNDIR -eq 1 ]; then
  175. create_default_dir "$CELERYD_PID_DIR"
  176. fi
  177. }
  178. create_paths() {
  179. create_default_dir "$CELERYD_LOG_DIR"
  180. create_default_dir "$CELERYD_PID_DIR"
  181. }
  182. export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
  183. _get_pidfiles () {
  184. # note: multi < 3.1.14 output to stderr, not stdout, hence the redirect.
  185. ${CELERYD_MULTI} expand "${CELERYD_PID_FILE}" ${CELERYD_NODES} 2>&1
  186. }
  187. _get_pids() {
  188. found_pids=0
  189. my_exitcode=0
  190. for pidfile in $(_get_pidfiles); do
  191. local pid=`cat "$pidfile"`
  192. local cleaned_pid=`echo "$pid" | sed -e 's/[^0-9]//g'`
  193. if [ -z "$pid" ] || [ "$cleaned_pid" != "$pid" ]; then
  194. echo "bad pid file ($pidfile)"
  195. one_failed=true
  196. my_exitcode=1
  197. else
  198. found_pids=1
  199. echo "$pid"
  200. fi
  201. if [ $found_pids -eq 0 ]; then
  202. echo "${SCRIPT_NAME}: All nodes down"
  203. exit $my_exitcode
  204. fi
  205. done
  206. }
  207. _chuid () {
  208. ${CELERYD_SU} ${CELERYD_SU_ARGS} "$CELERYD_USER" -c "$CELERYD_MULTI $*"
  209. }
  210. start_workers () {
  211. if [ ! -z "$CELERYD_ULIMIT" ]; then
  212. ulimit $CELERYD_ULIMIT
  213. fi
  214. _chuid $* start $CELERYD_NODES $DAEMON_OPTS \
  215. --pidfile="$CELERYD_PID_FILE" \
  216. --logfile="$CELERYD_LOG_FILE" \
  217. --loglevel="$CELERYD_LOG_LEVEL" \
  218. $CELERY_APP_ARG \
  219. $CELERYD_OPTS
  220. }
  221. dryrun () {
  222. (C_FAKEFORK=1 start_workers --verbose)
  223. }
  224. stop_workers () {
  225. _chuid stopwait $CELERYD_NODES --pidfile="$CELERYD_PID_FILE"
  226. }
  227. restart_workers () {
  228. _chuid restart $CELERYD_NODES $DAEMON_OPTS \
  229. --pidfile="$CELERYD_PID_FILE" \
  230. --logfile="$CELERYD_LOG_FILE" \
  231. --loglevel="$CELERYD_LOG_LEVEL" \
  232. $CELERY_APP_ARG \
  233. $CELERYD_OPTS
  234. }
  235. kill_workers() {
  236. _chuid kill $CELERYD_NODES --pidfile="$CELERYD_PID_FILE"
  237. }
  238. restart_workers_graceful () {
  239. echo "WARNING: Use with caution in production"
  240. echo "The workers will attempt to restart, but they may not be able to."
  241. local worker_pids=
  242. worker_pids=`_get_pids`
  243. [ "$one_failed" ] && exit 1
  244. for worker_pid in $worker_pids; do
  245. local failed=
  246. kill -HUP $worker_pid 2> /dev/null || failed=true
  247. if [ "$failed" ]; then
  248. echo "${SCRIPT_NAME} worker (pid $worker_pid) could not be restarted"
  249. one_failed=true
  250. else
  251. echo "${SCRIPT_NAME} worker (pid $worker_pid) received SIGHUP"
  252. fi
  253. done
  254. [ "$one_failed" ] && exit 1 || exit 0
  255. }
  256. check_status () {
  257. my_exitcode=0
  258. found_pids=0
  259. local one_failed=
  260. for pidfile in $(_get_pidfiles); do
  261. if [ ! -r $pidfile ]; then
  262. echo "${SCRIPT_NAME} down: no pidfiles found"
  263. one_failed=true
  264. break
  265. fi
  266. local node=`basename "$pidfile" .pid`
  267. local pid=`cat "$pidfile"`
  268. local cleaned_pid=`echo "$pid" | sed -e 's/[^0-9]//g'`
  269. if [ -z "$pid" ] || [ "$cleaned_pid" != "$pid" ]; then
  270. echo "bad pid file ($pidfile)"
  271. one_failed=true
  272. else
  273. local failed=
  274. kill -0 $pid 2> /dev/null || failed=true
  275. if [ "$failed" ]; then
  276. echo "${SCRIPT_NAME} (node $node) (pid $pid) is down, but pidfile exists!"
  277. one_failed=true
  278. else
  279. echo "${SCRIPT_NAME} (node $node) (pid $pid) is up..."
  280. fi
  281. fi
  282. done
  283. [ "$one_failed" ] && exit 1 || exit 0
  284. }
  285. case "$1" in
  286. start)
  287. check_dev_null
  288. check_paths
  289. start_workers
  290. ;;
  291. stop)
  292. check_dev_null
  293. check_paths
  294. stop_workers
  295. ;;
  296. reload|force-reload)
  297. echo "Use restart"
  298. ;;
  299. status)
  300. check_status
  301. ;;
  302. restart)
  303. check_dev_null
  304. check_paths
  305. restart_workers
  306. ;;
  307. graceful)
  308. check_dev_null
  309. restart_workers_graceful
  310. ;;
  311. kill)
  312. check_dev_null
  313. kill_workers
  314. ;;
  315. dryrun)
  316. check_dev_null
  317. dryrun
  318. ;;
  319. try-restart)
  320. check_dev_null
  321. check_paths
  322. restart_workers
  323. ;;
  324. create-paths)
  325. check_dev_null
  326. create_paths
  327. ;;
  328. check-paths)
  329. check_dev_null
  330. check_paths
  331. ;;
  332. *)
  333. echo "Usage: /etc/init.d/${SCRIPT_NAME} {start|stop|restart|graceful|kill|dryrun|create-paths}"
  334. exit 64 # EX_USAGE
  335. ;;
  336. esac
  337. exit 0