celeryd 11 KB

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