celerybeat 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #!/bin/sh
  2. # ============================================
  3. # celerybeat - Starts the Celery periodic task scheduler.
  4. # ============================================
  5. #
  6. # :Usage: /etc/init.d/celerybeat {start|stop|restart|status}
  7. # :Configuration file: /etc/sysconfig/celerybeat
  8. #
  9. # See http://docs.celeryproject.org/en/latest/tutorials/daemonizing.html
  10. ### BEGIN INIT INFO
  11. # Provides: celerybeat
  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, do NOT copy this script. Instead,
  21. # symlink it. I.e., if my new application, "little-worker" needs an init, I
  22. # should just use:
  23. #
  24. # ln -s /etc/init.d/celerybeat /etc/init.d/little-worker
  25. #
  26. # You can then configure this by manipulating /etc/sysconfig/little-worker.
  27. #
  28. # Setting `prog` here allows you to symlink this init script, making it easy
  29. # to run multiple processes on the system.
  30. # If we're invoked via SysV-style runlevel scripts we need to follow the
  31. # link from rcX.d before working out the script name.
  32. if [[ `dirname $0` == /etc/rc*.d ]]; then
  33. target="$(readlink $0)"
  34. else
  35. target=$0
  36. fi
  37. prog="$(basename $target)"
  38. # Source the centos service helper functions
  39. source /etc/init.d/functions
  40. # NOTE: "set -e" does not work with the above functions,
  41. # which use non-zero return codes as non-error return conditions
  42. # some commands work asyncronously, so we'll wait this many seconds
  43. SLEEP_SECONDS=5
  44. DEFAULT_PID_FILE="/var/run/celery/$prog.pid"
  45. DEFAULT_LOG_FILE="/var/log/celery/$prog.log"
  46. DEFAULT_LOG_LEVEL="INFO"
  47. DEFAULT_NODES="celery"
  48. CELERY_DEFAULTS=${CELERY_DEFAULTS:-"/etc/sysconfig/$prog"}
  49. test -f "$CELERY_DEFAULTS" && . "$CELERY_DEFAULTS"
  50. # Set CELERY_CREATE_DIRS to always create log/pid dirs.
  51. CELERY_CREATE_DIRS=${CELERY_CREATE_DIRS:-0}
  52. CELERY_CREATE_RUNDIR=$CELERY_CREATE_DIRS
  53. CELERY_CREATE_LOGDIR=$CELERY_CREATE_DIRS
  54. if [ -z "$CELERYBEAT_PID_FILE" ]; then
  55. CELERYBEAT_PID_FILE="$DEFAULT_PID_FILE"
  56. CELERY_CREATE_RUNDIR=1
  57. fi
  58. if [ -z "$CELERYBEAT_LOG_FILE" ]; then
  59. CELERYBEAT_LOG_FILE="$DEFAULT_LOG_FILE"
  60. CELERY_CREATE_LOGDIR=1
  61. fi
  62. CELERYBEAT_LOG_LEVEL=${CELERYBEAT_LOG_LEVEL:-${CELERYBEAT_LOGLEVEL:-$DEFAULT_LOG_LEVEL}}
  63. CELERYBEAT=${CELERYBEAT:-"${CELERY_BIN} beat"}
  64. CELERYBEAT=${CELERYBEAT:-$DEFAULT_CELERYBEAT}
  65. CELERYBEAT_NODES=${CELERYBEAT_NODES:-$DEFAULT_NODES}
  66. # This is used to change how Celery loads in the configs. It does not need to
  67. # be set to be run.
  68. export CELERY_LOADER
  69. if [ -n "$2" ]; then
  70. CELERYBEAT_OPTS="$CELERYBEAT_OPTS $2"
  71. fi
  72. CELERYBEAT_OPTS=${CELERYBEAT_OPTS:-"--app=$CELERY_APP"}
  73. CELERYBEAT_LOG_DIR=`dirname $CELERYBEAT_LOG_FILE`
  74. CELERYBEAT_PID_DIR=`dirname $CELERYBEAT_PID_FILE`
  75. # Extra start-stop-daemon options, like user/group.
  76. if [ -n "$CELERYBEAT_USER" ]; then
  77. DAEMON_OPTS="$DAEMON_OPTS --uid=$CELERYBEAT_USER"
  78. fi
  79. if [ -n "$CELERYBEAT_GROUP" ]; then
  80. DAEMON_OPTS="$DAEMON_OPTS --gid=$CELERYBEAT_GROUP"
  81. fi
  82. if [ -n "$CELERYBEAT_CHDIR" ]; then
  83. DAEMON_OPTS="$DAEMON_OPTS --workdir=$CELERYBEAT_CHDIR"
  84. fi
  85. check_dev_null() {
  86. if [ ! -c /dev/null ]; then
  87. echo "/dev/null is not a character device!"
  88. exit 75 # EX_TEMPFAIL
  89. fi
  90. }
  91. maybe_die() {
  92. if [ $? -ne 0 ]; then
  93. echo "Exiting: $* (errno $?)"
  94. exit 77 # EX_NOPERM
  95. fi
  96. }
  97. create_default_dir() {
  98. if [ ! -d "$1" ]; then
  99. echo "- Creating default directory: '$1'"
  100. mkdir -p "$1"
  101. maybe_die "Couldn't create directory $1"
  102. echo "- Changing permissions of '$1' to 02755"
  103. chmod 02755 "$1"
  104. maybe_die "Couldn't change permissions for $1"
  105. if [ -n "$CELERYBEAT_USER" ]; then
  106. echo "- Changing owner of '$1' to '$CELERYBEAT_USER'"
  107. chown "$CELERYBEAT_USER" "$1"
  108. maybe_die "Couldn't change owner of $1"
  109. fi
  110. if [ -n "$CELERYBEAT_GROUP" ]; then
  111. echo "- Changing group of '$1' to '$CELERYBEAT_GROUP'"
  112. chgrp "$CELERYBEAT_GROUP" "$1"
  113. maybe_die "Couldn't change group of $1"
  114. fi
  115. fi
  116. }
  117. check_paths() {
  118. if [ $CELERY_CREATE_LOGDIR -eq 1 ]; then
  119. create_default_dir "$CELERYBEAT_LOG_DIR"
  120. fi
  121. if [ $CELERY_CREATE_RUNDIR -eq 1 ]; then
  122. create_default_dir "$CELERYBEAT_PID_DIR"
  123. fi
  124. }
  125. create_paths() {
  126. create_default_dir "$CELERYBEAT_LOG_DIR"
  127. create_default_dir "$CELERYBEAT_PID_DIR"
  128. }
  129. export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
  130. stop() {
  131. [[ ! -f "$CELERYBEAT_PID_FILE" ]] && echo "$prog is stopped" && return 0
  132. local one_failed=
  133. echo -n $"Stopping $prog: "
  134. # killproc comes from 'functions' and brings three nice features:
  135. # 1. sending TERM, sleeping, then sleeping more if needed, then sending KILL
  136. # 2. handling 'success' and 'failure' output
  137. # 3. removes stale pid files, if any remain
  138. killproc -p "$CELERYBEAT_PID_FILE" -d "$SLEEP_SECONDS" $prog || one_failed=true
  139. echo
  140. [[ "$one_failed" ]] && return 1 || return 0
  141. }
  142. start() {
  143. echo -n $"Starting $prog: "
  144. # If Celery is already running, bail out
  145. if [[ -f "$CELERYBEAT_PID_FILE" ]]; then
  146. echo -n "$prog is already running. Use 'restart'."
  147. failure
  148. echo
  149. return 1
  150. fi
  151. $CELERYBEAT $CELERYBEAT_OPTS $DAEMON_OPTS --detach \
  152. --pidfile="$CELERYBEAT_PID_FILE" \
  153. --logfile="$CELERYBEAT_LOG_FILE" \
  154. --loglevel="$CELERYBEAT_LOG_LEVEL"
  155. if [[ "$?" == "0" ]]; then
  156. # Sleep a few seconds to give Celery a chance to initialize itself.
  157. # This is useful to prevent scripts following this one from trying to
  158. # use Celery (or its pid files) too early.
  159. sleep $SLEEP_SECONDS
  160. if [[ -f "$CELERYBEAT_PID_FILE" ]]; then
  161. success
  162. echo
  163. return 0
  164. else # celerybeat succeeded but no pid files found
  165. failure
  166. fi
  167. else # celerybeat did not succeed
  168. failure
  169. fi
  170. echo
  171. return 1
  172. }
  173. check_status() {
  174. status -p "$CELERYBEAT_PID_FILE" $"$prog" || return 1
  175. return 0
  176. }
  177. case "$1" in
  178. start)
  179. check_dev_null
  180. check_paths
  181. start
  182. ;;
  183. stop)
  184. check_dev_null
  185. check_paths
  186. stop
  187. ;;
  188. status)
  189. check_status
  190. ;;
  191. restart)
  192. check_dev_null
  193. check_paths
  194. stop && start
  195. ;;
  196. *)
  197. echo "Usage: /etc/init.d/$prog {start|stop|restart|status}"
  198. exit 3
  199. ;;
  200. esac
  201. exit $?