celerybeat 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #!/bin/bash
  2. # =========================================================
  3. # celerybeat - Starts the Celery periodic task scheduler.
  4. # =========================================================
  5. #
  6. # :Usage: /etc/init.d/celerybeat {start|stop|force-reload|restart|try-restart|status}
  7. # :Configuration file: /etc/default/celerybeat or /etc/default/celeryd
  8. #
  9. # See http://docs.celeryproject.org/en/latest/tutorials/daemonizing.html#generic-init-scripts
  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 periodic task scheduler
  17. ### END INIT INFO
  18. # Cannot use set -e/bash -e since the kill -0 command will abort
  19. # abnormally in the absence of a valid process ID.
  20. #set -e
  21. VERSION=10.0
  22. echo "celery init v${VERSION}."
  23. if [ $(id -u) -ne 0 ]; then
  24. echo "Error: This program can only be used by the root user."
  25. echo " Unpriviliged users must use 'celery beat --detach'"
  26. exit 1
  27. fi
  28. # May be a runlevel symlink (e.g. S02celeryd)
  29. if [ -L "$0" ]; then
  30. SCRIPT_FILE=$(readlink "$0")
  31. else
  32. SCRIPT_FILE="$0"
  33. fi
  34. SCRIPT_NAME="$(basename "$SCRIPT_FILE")"
  35. # /etc/init.d/celerybeat: start and stop the celery periodic task scheduler daemon.
  36. # Make sure executable configuration script is owned by root
  37. _config_sanity() {
  38. local path="$1"
  39. local owner=$(ls -ld "$path" | awk '{print $3}')
  40. local iwgrp=$(ls -ld "$path" | cut -b 6)
  41. local iwoth=$(ls -ld "$path" | cut -b 9)
  42. if [ "$(id -u $owner)" != "0" ]; then
  43. echo "Error: Config script '$path' must be owned by root!"
  44. echo
  45. echo "Resolution:"
  46. echo "Review the file carefully and make sure it has not been "
  47. echo "modified with mailicious intent. When sure the "
  48. echo "script is safe to execute with superuser privileges "
  49. echo "you can change ownership of the script:"
  50. echo " $ sudo chown root '$path'"
  51. exit 1
  52. fi
  53. if [ "$iwoth" != "-" ]; then # S_IWOTH
  54. echo "Error: Config script '$path' cannot be writable by others!"
  55. echo
  56. echo "Resolution:"
  57. echo "Review the file carefully and make sure it has not been "
  58. echo "modified with malicious intent. When sure the "
  59. echo "script is safe to execute with superuser privileges "
  60. echo "you can change the scripts permissions:"
  61. echo " $ sudo chmod 640 '$path'"
  62. exit 1
  63. fi
  64. if [ "$iwgrp" != "-" ]; then # S_IWGRP
  65. echo "Error: Config script '$path' cannot be writable by group!"
  66. echo
  67. echo "Resolution:"
  68. echo "Review the file carefully and make sure it has not been "
  69. echo "modified with malicious intent. When sure the "
  70. echo "script is safe to execute with superuser privileges "
  71. echo "you can change the scripts permissions:"
  72. echo " $ sudo chmod 640 '$path'"
  73. exit 1
  74. fi
  75. }
  76. scripts=""
  77. if test -f /etc/default/celeryd; then
  78. scripts="/etc/default/celeryd"
  79. _config_sanity /etc/default/celeryd
  80. . /etc/default/celeryd
  81. fi
  82. EXTRA_CONFIG="/etc/default/${SCRIPT_NAME}"
  83. if test -f "$EXTRA_CONFIG"; then
  84. scripts="$scripts, $EXTRA_CONFIG"
  85. _config_sanity "$EXTRA_CONFIG"
  86. . "$EXTRA_CONFIG"
  87. fi
  88. echo "Using configuration: $scripts"
  89. CELERY_BIN=${CELERY_BIN:-"celery"}
  90. DEFAULT_USER="celery"
  91. DEFAULT_PID_FILE="/var/run/celery/beat.pid"
  92. DEFAULT_LOG_FILE="/var/log/celery/beat.log"
  93. DEFAULT_LOG_LEVEL="INFO"
  94. DEFAULT_CELERYBEAT="$CELERY_BIN beat"
  95. CELERYBEAT=${CELERYBEAT:-$DEFAULT_CELERYBEAT}
  96. CELERYBEAT_LOG_LEVEL=${CELERYBEAT_LOG_LEVEL:-${CELERYBEAT_LOGLEVEL:-$DEFAULT_LOG_LEVEL}}
  97. # Sets --app argument for CELERY_BIN
  98. CELERY_APP_ARG=""
  99. if [ ! -z "$CELERY_APP" ]; then
  100. CELERY_APP_ARG="--app=$CELERY_APP"
  101. fi
  102. CELERYBEAT_USER=${CELERYBEAT_USER:-${CELERYD_USER:-$DEFAULT_USER}}
  103. # Set CELERY_CREATE_DIRS to always create log/pid dirs.
  104. CELERY_CREATE_DIRS=${CELERY_CREATE_DIRS:-0}
  105. CELERY_CREATE_RUNDIR=$CELERY_CREATE_DIRS
  106. CELERY_CREATE_LOGDIR=$CELERY_CREATE_DIRS
  107. if [ -z "$CELERYBEAT_PID_FILE" ]; then
  108. CELERYBEAT_PID_FILE="$DEFAULT_PID_FILE"
  109. CELERY_CREATE_RUNDIR=1
  110. fi
  111. if [ -z "$CELERYBEAT_LOG_FILE" ]; then
  112. CELERYBEAT_LOG_FILE="$DEFAULT_LOG_FILE"
  113. CELERY_CREATE_LOGDIR=1
  114. fi
  115. export CELERY_LOADER
  116. CELERYBEAT_OPTS="$CELERYBEAT_OPTS -f $CELERYBEAT_LOG_FILE -l $CELERYBEAT_LOG_LEVEL"
  117. if [ -n "$2" ]; then
  118. CELERYBEAT_OPTS="$CELERYBEAT_OPTS $2"
  119. fi
  120. CELERYBEAT_LOG_DIR=`dirname $CELERYBEAT_LOG_FILE`
  121. CELERYBEAT_PID_DIR=`dirname $CELERYBEAT_PID_FILE`
  122. # Extra start-stop-daemon options, like user/group.
  123. CELERYBEAT_CHDIR=${CELERYBEAT_CHDIR:-$CELERYD_CHDIR}
  124. if [ -n "$CELERYBEAT_CHDIR" ]; then
  125. DAEMON_OPTS="$DAEMON_OPTS --workdir=$CELERYBEAT_CHDIR"
  126. fi
  127. export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
  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: $*"
  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 "$CELERYBEAT_USER" ]; then
  149. echo "- Changing owner of '$1' to '$CELERYBEAT_USER'"
  150. chown "$CELERYBEAT_USER" "$1"
  151. maybe_die "Couldn't change owner of $1"
  152. fi
  153. if [ -n "$CELERYBEAT_GROUP" ]; then
  154. echo "- Changing group of '$1' to '$CELERYBEAT_GROUP'"
  155. chgrp "$CELERYBEAT_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 "$CELERYBEAT_LOG_DIR"
  163. fi
  164. if [ $CELERY_CREATE_RUNDIR -eq 1 ]; then
  165. create_default_dir "$CELERYBEAT_PID_DIR"
  166. fi
  167. }
  168. create_paths () {
  169. create_default_dir "$CELERYBEAT_LOG_DIR"
  170. create_default_dir "$CELERYBEAT_PID_DIR"
  171. }
  172. wait_pid () {
  173. pid=$1
  174. forever=1
  175. i=0
  176. while [ $forever -gt 0 ]; do
  177. kill -0 $pid 1>/dev/null 2>&1
  178. if [ $? -eq 1 ]; then
  179. echo "OK"
  180. forever=0
  181. else
  182. kill -TERM "$pid"
  183. i=$((i + 1))
  184. if [ $i -gt 60 ]; then
  185. echo "ERROR"
  186. echo "Timed out while stopping (30s)"
  187. forever=0
  188. else
  189. sleep 0.5
  190. fi
  191. fi
  192. done
  193. }
  194. stop_beat () {
  195. echo -n "Stopping ${SCRIPT_NAME}... "
  196. if [ -f "$CELERYBEAT_PID_FILE" ]; then
  197. wait_pid $(cat "$CELERYBEAT_PID_FILE")
  198. else
  199. echo "NOT RUNNING"
  200. fi
  201. }
  202. _chuid () {
  203. su "$CELERYBEAT_USER" -c "$CELERYBEAT $*"
  204. }
  205. start_beat () {
  206. echo "Starting ${SCRIPT_NAME}..."
  207. _chuid $CELERY_APP_ARG $CELERYBEAT_OPTS $DAEMON_OPTS --detach \
  208. --pidfile="$CELERYBEAT_PID_FILE"
  209. }
  210. case "$1" in
  211. start)
  212. check_dev_null
  213. check_paths
  214. start_beat
  215. ;;
  216. stop)
  217. check_paths
  218. stop_beat
  219. ;;
  220. reload|force-reload)
  221. echo "Use start+stop"
  222. ;;
  223. restart)
  224. echo "Restarting celery periodic task scheduler"
  225. check_paths
  226. stop_beat
  227. check_dev_null
  228. start_beat
  229. ;;
  230. create-paths)
  231. check_dev_null
  232. create_paths
  233. ;;
  234. check-paths)
  235. check_dev_null
  236. check_paths
  237. ;;
  238. *)
  239. echo "Usage: /etc/init.d/${SCRIPT_NAME} {start|stop|restart|create-paths}"
  240. exit 64 # EX_USAGE
  241. ;;
  242. esac
  243. exit 0