celeryd 9.6 KB

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