celerybeat 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #!/bin/sh -e
  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.1
  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. origin_is_runlevel_dir () {
  29. set +e
  30. dirname $0 | grep -q "/etc/rc.\.d"
  31. echo $?
  32. }
  33. # Can be a runlevel symlink (e.g. S02celeryd)
  34. if [ $(origin_is_runlevel_dir) -eq 0 ]; then
  35. SCRIPT_FILE=$(readlink "$0")
  36. else
  37. SCRIPT_FILE="$0"
  38. fi
  39. SCRIPT_NAME="$(basename "$SCRIPT_FILE")"
  40. # /etc/init.d/celerybeat: start and stop the celery periodic task scheduler daemon.
  41. # Make sure executable configuration script is owned by root
  42. _config_sanity() {
  43. local path="$1"
  44. local owner=$(ls -ld "$path" | awk '{print $3}')
  45. local iwgrp=$(ls -ld "$path" | cut -b 6)
  46. local iwoth=$(ls -ld "$path" | cut -b 9)
  47. if [ "$(id -u $owner)" != "0" ]; then
  48. echo "Error: Config script '$path' must be owned by root!"
  49. echo
  50. echo "Resolution:"
  51. echo "Review the file carefully and make sure it has not been "
  52. echo "modified with mailicious intent. When sure the "
  53. echo "script is safe to execute with superuser privileges "
  54. echo "you can change ownership of the script:"
  55. echo " $ sudo chown root '$path'"
  56. exit 1
  57. fi
  58. if [ "$iwoth" != "-" ]; then # S_IWOTH
  59. echo "Error: Config script '$path' cannot be writable by others!"
  60. echo
  61. echo "Resolution:"
  62. echo "Review the file carefully and make sure it has not been "
  63. echo "modified with malicious intent. When sure the "
  64. echo "script is safe to execute with superuser privileges "
  65. echo "you can change the scripts permissions:"
  66. echo " $ sudo chmod 640 '$path'"
  67. exit 1
  68. fi
  69. if [ "$iwgrp" != "-" ]; then # S_IWGRP
  70. echo "Error: Config script '$path' cannot be writable by group!"
  71. echo
  72. echo "Resolution:"
  73. echo "Review the file carefully and make sure it has not been "
  74. echo "modified with malicious intent. When sure the "
  75. echo "script is safe to execute with superuser privileges "
  76. echo "you can change the scripts permissions:"
  77. echo " $ sudo chmod 640 '$path'"
  78. exit 1
  79. fi
  80. }
  81. scripts=""
  82. if test -f /etc/default/celeryd; then
  83. scripts="/etc/default/celeryd"
  84. _config_sanity /etc/default/celeryd
  85. . /etc/default/celeryd
  86. fi
  87. EXTRA_CONFIG="/etc/default/${SCRIPT_NAME}"
  88. if test -f "$EXTRA_CONFIG"; then
  89. scripts="$scripts, $EXTRA_CONFIG"
  90. _config_sanity "$EXTRA_CONFIG"
  91. . "$EXTRA_CONFIG"
  92. fi
  93. echo "Using configuration: $scripts"
  94. CELERY_BIN=${CELERY_BIN:-"celery"}
  95. DEFAULT_USER="celery"
  96. DEFAULT_PID_FILE="/var/run/celery/beat.pid"
  97. DEFAULT_LOG_FILE="/var/log/celery/beat.log"
  98. DEFAULT_LOG_LEVEL="INFO"
  99. DEFAULT_CELERYBEAT="$CELERY_BIN beat"
  100. CELERYBEAT=${CELERYBEAT:-$DEFAULT_CELERYBEAT}
  101. CELERYBEAT_LOG_LEVEL=${CELERYBEAT_LOG_LEVEL:-${CELERYBEAT_LOGLEVEL:-$DEFAULT_LOG_LEVEL}}
  102. # Sets --app argument for CELERY_BIN
  103. CELERY_APP_ARG=""
  104. if [ ! -z "$CELERY_APP" ]; then
  105. CELERY_APP_ARG="--app=$CELERY_APP"
  106. fi
  107. CELERYBEAT_USER=${CELERYBEAT_USER:-${CELERYD_USER:-$DEFAULT_USER}}
  108. # Set CELERY_CREATE_DIRS to always create log/pid dirs.
  109. CELERY_CREATE_DIRS=${CELERY_CREATE_DIRS:-0}
  110. CELERY_CREATE_RUNDIR=$CELERY_CREATE_DIRS
  111. CELERY_CREATE_LOGDIR=$CELERY_CREATE_DIRS
  112. if [ -z "$CELERYBEAT_PID_FILE" ]; then
  113. CELERYBEAT_PID_FILE="$DEFAULT_PID_FILE"
  114. CELERY_CREATE_RUNDIR=1
  115. fi
  116. if [ -z "$CELERYBEAT_LOG_FILE" ]; then
  117. CELERYBEAT_LOG_FILE="$DEFAULT_LOG_FILE"
  118. CELERY_CREATE_LOGDIR=1
  119. fi
  120. export CELERY_LOADER
  121. CELERYBEAT_OPTS="$CELERYBEAT_OPTS -f $CELERYBEAT_LOG_FILE -l $CELERYBEAT_LOG_LEVEL"
  122. if [ -n "$2" ]; then
  123. CELERYBEAT_OPTS="$CELERYBEAT_OPTS $2"
  124. fi
  125. CELERYBEAT_LOG_DIR=`dirname $CELERYBEAT_LOG_FILE`
  126. CELERYBEAT_PID_DIR=`dirname $CELERYBEAT_PID_FILE`
  127. # Extra start-stop-daemon options, like user/group.
  128. CELERYBEAT_CHDIR=${CELERYBEAT_CHDIR:-$CELERYD_CHDIR}
  129. if [ -n "$CELERYBEAT_CHDIR" ]; then
  130. DAEMON_OPTS="$DAEMON_OPTS --workdir=$CELERYBEAT_CHDIR"
  131. fi
  132. export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
  133. check_dev_null() {
  134. if [ ! -c /dev/null ]; then
  135. echo "/dev/null is not a character device!"
  136. exit 75 # EX_TEMPFAIL
  137. fi
  138. }
  139. maybe_die() {
  140. if [ $? -ne 0 ]; then
  141. echo "Exiting: $*"
  142. exit 77 # EX_NOPERM
  143. fi
  144. }
  145. create_default_dir() {
  146. if [ ! -d "$1" ]; then
  147. echo "- Creating default directory: '$1'"
  148. mkdir -p "$1"
  149. maybe_die "Couldn't create directory $1"
  150. echo "- Changing permissions of '$1' to 02755"
  151. chmod 02755 "$1"
  152. maybe_die "Couldn't change permissions for $1"
  153. if [ -n "$CELERYBEAT_USER" ]; then
  154. echo "- Changing owner of '$1' to '$CELERYBEAT_USER'"
  155. chown "$CELERYBEAT_USER" "$1"
  156. maybe_die "Couldn't change owner of $1"
  157. fi
  158. if [ -n "$CELERYBEAT_GROUP" ]; then
  159. echo "- Changing group of '$1' to '$CELERYBEAT_GROUP'"
  160. chgrp "$CELERYBEAT_GROUP" "$1"
  161. maybe_die "Couldn't change group of $1"
  162. fi
  163. fi
  164. }
  165. check_paths() {
  166. if [ $CELERY_CREATE_LOGDIR -eq 1 ]; then
  167. create_default_dir "$CELERYBEAT_LOG_DIR"
  168. fi
  169. if [ $CELERY_CREATE_RUNDIR -eq 1 ]; then
  170. create_default_dir "$CELERYBEAT_PID_DIR"
  171. fi
  172. }
  173. create_paths () {
  174. create_default_dir "$CELERYBEAT_LOG_DIR"
  175. create_default_dir "$CELERYBEAT_PID_DIR"
  176. }
  177. wait_pid () {
  178. pid=$1
  179. forever=1
  180. i=0
  181. while [ $forever -gt 0 ]; do
  182. kill -0 $pid 1>/dev/null 2>&1
  183. if [ $? -eq 1 ]; then
  184. echo "OK"
  185. forever=0
  186. else
  187. kill -TERM "$pid"
  188. i=$((i + 1))
  189. if [ $i -gt 60 ]; then
  190. echo "ERROR"
  191. echo "Timed out while stopping (30s)"
  192. forever=0
  193. else
  194. sleep 0.5
  195. fi
  196. fi
  197. done
  198. }
  199. stop_beat () {
  200. echo -n "Stopping ${SCRIPT_NAME}... "
  201. if [ -f "$CELERYBEAT_PID_FILE" ]; then
  202. wait_pid $(cat "$CELERYBEAT_PID_FILE")
  203. else
  204. echo "NOT RUNNING"
  205. fi
  206. }
  207. _chuid () {
  208. su "$CELERYBEAT_USER" -c "$CELERYBEAT $*"
  209. }
  210. start_beat () {
  211. echo "Starting ${SCRIPT_NAME}..."
  212. _chuid $CELERY_APP_ARG $CELERYBEAT_OPTS $DAEMON_OPTS --detach \
  213. --pidfile="$CELERYBEAT_PID_FILE"
  214. }
  215. check_status () {
  216. local failed=
  217. local pid_file=$CELERYBEAT_PID_FILE
  218. if [ ! -e $pid_file ]; then
  219. echo "${SCRIPT_NAME} is down: no pid file found"
  220. failed=true
  221. elif [ ! -r $pid_file ]; then
  222. echo "${SCRIPT_NAME} is in unknown state, user cannot read pid file."
  223. failed=true
  224. else
  225. local pid=`cat "$pid_file"`
  226. local cleaned_pid=`echo "$pid" | sed -e 's/[^0-9]//g'`
  227. if [ -z "$pid" ] || [ "$cleaned_pid" != "$pid" ]; then
  228. echo "${SCRIPT_NAME}: bad pid file ($pid_file)"
  229. failed=true
  230. else
  231. local failed=
  232. kill -0 $pid 2> /dev/null || failed=true
  233. if [ "$failed" ]; then
  234. echo "${SCRIPT_NAME} (pid $pid) is down, but pid file exists!"
  235. failed=true
  236. else
  237. echo "${SCRIPT_NAME} (pid $pid) is up..."
  238. fi
  239. fi
  240. fi
  241. [ "$failed" ] && exit 1 || exit 0
  242. }
  243. case "$1" in
  244. start)
  245. check_dev_null
  246. check_paths
  247. start_beat
  248. ;;
  249. stop)
  250. check_paths
  251. stop_beat
  252. ;;
  253. reload|force-reload)
  254. echo "Use start+stop"
  255. ;;
  256. status)
  257. check_status
  258. ;;
  259. restart)
  260. echo "Restarting celery periodic task scheduler"
  261. check_paths
  262. stop_beat
  263. check_dev_null
  264. start_beat
  265. ;;
  266. create-paths)
  267. check_dev_null
  268. create_paths
  269. ;;
  270. check-paths)
  271. check_dev_null
  272. check_paths
  273. ;;
  274. *)
  275. echo "Usage: /etc/init.d/${SCRIPT_NAME} {start|stop|restart|create-paths|status}"
  276. exit 64 # EX_USAGE
  277. ;;
  278. esac
  279. exit 0