celerybeat 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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/userguide/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 hasn't 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 hasn't 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 hasn't 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. CELERYBEAT_SU=${CELERYBEAT_SU:-"su"}
  103. CELERYBEAT_SU_ARGS=${CELERYBEAT_SU_ARGS:-""}
  104. # Sets --app argument for CELERY_BIN
  105. CELERY_APP_ARG=""
  106. if [ ! -z "$CELERY_APP" ]; then
  107. CELERY_APP_ARG="--app=$CELERY_APP"
  108. fi
  109. CELERYBEAT_USER=${CELERYBEAT_USER:-${CELERYD_USER:-$DEFAULT_USER}}
  110. # Set CELERY_CREATE_DIRS to always create log/pid dirs.
  111. CELERY_CREATE_DIRS=${CELERY_CREATE_DIRS:-0}
  112. CELERY_CREATE_RUNDIR=$CELERY_CREATE_DIRS
  113. CELERY_CREATE_LOGDIR=$CELERY_CREATE_DIRS
  114. if [ -z "$CELERYBEAT_PID_FILE" ]; then
  115. CELERYBEAT_PID_FILE="$DEFAULT_PID_FILE"
  116. CELERY_CREATE_RUNDIR=1
  117. fi
  118. if [ -z "$CELERYBEAT_LOG_FILE" ]; then
  119. CELERYBEAT_LOG_FILE="$DEFAULT_LOG_FILE"
  120. CELERY_CREATE_LOGDIR=1
  121. fi
  122. export CELERY_LOADER
  123. CELERYBEAT_OPTS="$CELERYBEAT_OPTS -f $CELERYBEAT_LOG_FILE -l $CELERYBEAT_LOG_LEVEL"
  124. if [ -n "$2" ]; then
  125. CELERYBEAT_OPTS="$CELERYBEAT_OPTS $2"
  126. fi
  127. CELERYBEAT_LOG_DIR=`dirname $CELERYBEAT_LOG_FILE`
  128. CELERYBEAT_PID_DIR=`dirname $CELERYBEAT_PID_FILE`
  129. # Extra start-stop-daemon options, like user/group.
  130. CELERYBEAT_CHDIR=${CELERYBEAT_CHDIR:-$CELERYD_CHDIR}
  131. if [ -n "$CELERYBEAT_CHDIR" ]; then
  132. DAEMON_OPTS="$DAEMON_OPTS --workdir=$CELERYBEAT_CHDIR"
  133. fi
  134. export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
  135. check_dev_null() {
  136. if [ ! -c /dev/null ]; then
  137. echo "/dev/null is not a character device!"
  138. exit 75 # EX_TEMPFAIL
  139. fi
  140. }
  141. maybe_die() {
  142. if [ $? -ne 0 ]; then
  143. echo "Exiting: $*"
  144. exit 77 # EX_NOPERM
  145. fi
  146. }
  147. create_default_dir() {
  148. if [ ! -d "$1" ]; then
  149. echo "- Creating default directory: '$1'"
  150. mkdir -p "$1"
  151. maybe_die "Couldn't create directory $1"
  152. echo "- Changing permissions of '$1' to 02755"
  153. chmod 02755 "$1"
  154. maybe_die "Couldn't change permissions for $1"
  155. if [ -n "$CELERYBEAT_USER" ]; then
  156. echo "- Changing owner of '$1' to '$CELERYBEAT_USER'"
  157. chown "$CELERYBEAT_USER" "$1"
  158. maybe_die "Couldn't change owner of $1"
  159. fi
  160. if [ -n "$CELERYBEAT_GROUP" ]; then
  161. echo "- Changing group of '$1' to '$CELERYBEAT_GROUP'"
  162. chgrp "$CELERYBEAT_GROUP" "$1"
  163. maybe_die "Couldn't change group of $1"
  164. fi
  165. fi
  166. }
  167. check_paths() {
  168. if [ $CELERY_CREATE_LOGDIR -eq 1 ]; then
  169. create_default_dir "$CELERYBEAT_LOG_DIR"
  170. fi
  171. if [ $CELERY_CREATE_RUNDIR -eq 1 ]; then
  172. create_default_dir "$CELERYBEAT_PID_DIR"
  173. fi
  174. }
  175. create_paths () {
  176. create_default_dir "$CELERYBEAT_LOG_DIR"
  177. create_default_dir "$CELERYBEAT_PID_DIR"
  178. }
  179. is_running() {
  180. pid=$1
  181. ps $pid > /dev/null 2>&1
  182. }
  183. wait_pid () {
  184. pid=$1
  185. forever=1
  186. i=0
  187. while [ $forever -gt 0 ]; do
  188. if ! is_running $pid; then
  189. echo "OK"
  190. forever=0
  191. else
  192. kill -TERM "$pid"
  193. i=$((i + 1))
  194. if [ $i -gt 60 ]; then
  195. echo "ERROR"
  196. echo "Timed out while stopping (30s)"
  197. forever=0
  198. else
  199. sleep 0.5
  200. fi
  201. fi
  202. done
  203. }
  204. stop_beat () {
  205. echo -n "Stopping ${SCRIPT_NAME}... "
  206. if [ -f "$CELERYBEAT_PID_FILE" ]; then
  207. wait_pid $(cat "$CELERYBEAT_PID_FILE")
  208. else
  209. echo "NOT RUNNING"
  210. fi
  211. }
  212. _chuid () {
  213. ${CELERYBEAT_SU} ${CELERYBEAT_SU_ARGS} \
  214. "$CELERYBEAT_USER" -c "$CELERYBEAT $*"
  215. }
  216. start_beat () {
  217. echo "Starting ${SCRIPT_NAME}..."
  218. _chuid $CELERY_APP_ARG $CELERYBEAT_OPTS $DAEMON_OPTS --detach \
  219. --pidfile="$CELERYBEAT_PID_FILE"
  220. }
  221. check_status () {
  222. local failed=
  223. local pid_file=$CELERYBEAT_PID_FILE
  224. if [ ! -e $pid_file ]; then
  225. echo "${SCRIPT_NAME} is down: no pid file found"
  226. failed=true
  227. elif [ ! -r $pid_file ]; then
  228. echo "${SCRIPT_NAME} is in unknown state, user cannot read pid file."
  229. failed=true
  230. else
  231. local pid=`cat "$pid_file"`
  232. local cleaned_pid=`echo "$pid" | sed -e 's/[^0-9]//g'`
  233. if [ -z "$pid" ] || [ "$cleaned_pid" != "$pid" ]; then
  234. echo "${SCRIPT_NAME}: bad pid file ($pid_file)"
  235. failed=true
  236. else
  237. local failed=
  238. kill -0 $pid 2> /dev/null || failed=true
  239. if [ "$failed" ]; then
  240. echo "${SCRIPT_NAME} (pid $pid) is down, but pid file exists!"
  241. failed=true
  242. else
  243. echo "${SCRIPT_NAME} (pid $pid) is up..."
  244. fi
  245. fi
  246. fi
  247. [ "$failed" ] && exit 1 || exit 0
  248. }
  249. case "$1" in
  250. start)
  251. check_dev_null
  252. check_paths
  253. start_beat
  254. ;;
  255. stop)
  256. check_paths
  257. stop_beat
  258. ;;
  259. reload|force-reload)
  260. echo "Use start+stop"
  261. ;;
  262. status)
  263. check_status
  264. ;;
  265. restart)
  266. echo "Restarting celery periodic task scheduler"
  267. check_paths
  268. stop_beat && check_dev_null && start_beat
  269. ;;
  270. create-paths)
  271. check_dev_null
  272. create_paths
  273. ;;
  274. check-paths)
  275. check_dev_null
  276. check_paths
  277. ;;
  278. *)
  279. echo "Usage: /etc/init.d/${SCRIPT_NAME} {start|stop|restart|create-paths|status}"
  280. exit 64 # EX_USAGE
  281. ;;
  282. esac
  283. exit 0