celeryd 11 KB

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