celeryd 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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.0
  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.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_pids() {
  174. found_pids=0
  175. my_exitcode=0
  176. for pid_file in "$CELERYD_PID_DIR"/*.pid; do
  177. local pid=`cat "$pid_file"`
  178. local cleaned_pid=`echo "$pid" | sed -e 's/[^0-9]//g'`
  179. if [ -z "$pid" ] || [ "$cleaned_pid" != "$pid" ]; then
  180. echo "bad pid file ($pid_file)"
  181. one_failed=true
  182. my_exitcode=1
  183. else
  184. found_pids=1
  185. echo "$pid"
  186. fi
  187. if [ $found_pids -eq 0 ]; then
  188. echo "${SCRIPT_NAME}: All nodes down"
  189. exit $my_exitcode
  190. fi
  191. done
  192. }
  193. _chuid () {
  194. su "$CELERYD_USER" -c "$CELERYD_MULTI $*"
  195. }
  196. start_workers () {
  197. if [ ! -z "$CELERYD_ULIMIT" ]; then
  198. ulimit $CELERYD_ULIMIT
  199. fi
  200. _chuid $* start $CELERYD_NODES $DAEMON_OPTS \
  201. --pidfile="$CELERYD_PID_FILE" \
  202. --logfile="$CELERYD_LOG_FILE" \
  203. --loglevel="$CELERYD_LOG_LEVEL" \
  204. $CELERY_APP_ARG \
  205. $CELERYD_OPTS
  206. }
  207. dryrun () {
  208. (C_FAKEFORK=1 start_workers --verbose)
  209. }
  210. stop_workers () {
  211. _chuid stopwait $CELERYD_NODES --pidfile="$CELERYD_PID_FILE"
  212. }
  213. restart_workers () {
  214. _chuid restart $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. kill_workers() {
  222. _chuid kill $CELERYD_NODES --pidfile="$CELERYD_PID_FILE"
  223. }
  224. restart_workers_graceful () {
  225. local worker_pids=
  226. worker_pids=`_get_pids`
  227. [ "$one_failed" ] && exit 1
  228. for worker_pid in $worker_pids; do
  229. local failed=
  230. kill -HUP $worker_pid 2> /dev/null || failed=true
  231. if [ "$failed" ]; then
  232. echo "${SCRIPT_NAME} worker (pid $worker_pid) could not be restarted"
  233. one_failed=true
  234. else
  235. echo "${SCRIPT_NAME} worker (pid $worker_pid) received SIGHUP"
  236. fi
  237. done
  238. [ "$one_failed" ] && exit 1 || exit 0
  239. }
  240. check_status () {
  241. my_exitcode=0
  242. found_pids=0
  243. local one_failed=
  244. for pid_file in "$CELERYD_PID_DIR"/*.pid; do
  245. if [ ! -r $pid_file ]; then
  246. echo "${SCRIPT_NAME} is stopped: no pids were found"
  247. one_failed=true
  248. break
  249. fi
  250. local node=`basename "$pid_file" .pid`
  251. local pid=`cat "$pid_file"`
  252. local cleaned_pid=`echo "$pid" | sed -e 's/[^0-9]//g'`
  253. if [ -z "$pid" ] || [ "$cleaned_pid" != "$pid" ]; then
  254. echo "bad pid file ($pid_file)"
  255. one_failed=true
  256. else
  257. local failed=
  258. kill -0 $pid 2> /dev/null || failed=true
  259. if [ "$failed" ]; then
  260. echo "${SCRIPT_NAME} (node $node) (pid $pid) is stopped, but pid file exists!"
  261. one_failed=true
  262. else
  263. echo "${SCRIPT_NAME} (node $node) (pid $pid) is running..."
  264. fi
  265. fi
  266. done
  267. [ "$one_failed" ] && exit 1 || exit 0
  268. }
  269. case "$1" in
  270. start)
  271. check_dev_null
  272. check_paths
  273. start_workers
  274. ;;
  275. stop)
  276. check_dev_null
  277. check_paths
  278. stop_workers
  279. ;;
  280. reload|force-reload)
  281. echo "Use restart"
  282. ;;
  283. status)
  284. check_status
  285. ;;
  286. restart)
  287. check_dev_null
  288. check_paths
  289. restart_workers
  290. ;;
  291. graceful)
  292. check_dev_null
  293. restart_workers_graceful
  294. ;;
  295. kill)
  296. check_dev_null
  297. kill_workers
  298. ;;
  299. dryrun)
  300. check_dev_null
  301. dryrun
  302. ;;
  303. try-restart)
  304. check_dev_null
  305. check_paths
  306. restart_workers
  307. ;;
  308. create-paths)
  309. check_dev_null
  310. create_paths
  311. ;;
  312. check-paths)
  313. check_dev_null
  314. check_paths
  315. ;;
  316. *)
  317. echo "Usage: /etc/init.d/${SCRIPT_NAME} {start|stop|restart|graceful|kill|dryrun|create-paths}"
  318. exit 64 # EX_USAGE
  319. ;;
  320. esac
  321. exit 0