Debian Codegraph @

extra/generic-init.d/celerybeat

raw ◀ ▶
2 Debian patches touch this file
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 https://docs.celeryq.dev/en/latest/userguide/daemonizing.html#generic-init-scripts
10
11 ### BEGIN INIT INFO
12 # Provides: celerybeat
13 # Required-Start: $network $local_fs $remote_fs
14 # Required-Stop: $network $local_fs $remote_fs
15 # Default-Start: 2 3 4 5
16 # Default-Stop: 0 1 6
17 # Short-Description: celery periodic task scheduler
•18 # Description: Controls the Celery periodic task scheduler.
19 ### END INIT INFO
20
21 # Cannot use set -e/bash -e since the kill -0 command will abort
22 # abnormally in the absence of a valid process ID.
23 #set -e
•24
25 . /lib/lsb/init-functions
26
27 VERSION=10.1
28 echo "celery init v${VERSION}."
29
30 if [ $(id -u) -ne 0 ]; then
31 echo "Error: This program can only be used by the root user."
32 echo " Unprivileged users must use 'celery beat --detach'"
33 exit 1
34 fi
35
36 origin_is_runlevel_dir () {
37 set +e
38 dirname $0 | grep -q "/etc/rc.\.d"
39 echo $?
40 }
41
42 # Can be a runlevel symlink (e.g., S02celeryd)
43 if [ $(origin_is_runlevel_dir) -eq 0 ]; then
44 SCRIPT_FILE=$(readlink "$0")
45 else
46 SCRIPT_FILE="$0"
47 fi
48 SCRIPT_NAME="$(basename "$SCRIPT_FILE")"
49
50 # /etc/init.d/celerybeat: start and stop the celery periodic task scheduler daemon.
51
52 # Make sure executable configuration script is owned by root
53 _config_sanity() {
54 local path="$1"
55 local owner=$(ls -ld "$path" | awk '{print $3}')
56 local iwgrp=$(ls -ld "$path" | cut -b 6)
57 local iwoth=$(ls -ld "$path" | cut -b 9)
58
59 if [ "$(id -u $owner)" != "0" ]; then
60 echo "Error: Config script '$path' must be owned by root!"
61 echo
62 echo "Resolution:"
63 echo "Review the file carefully, and make sure it hasn't been "
64 echo "modified with malicious intent. When sure the "
65 echo "script is safe to execute with superuser privileges "
66 echo "you can change ownership of the script:"
67 echo " $ sudo chown root '$path'"
68 exit 1
69 fi
70
71 if [ "$iwoth" != "-" ]; then # S_IWOTH
72 echo "Error: Config script '$path' cannot be writable by others!"
73 echo
74 echo "Resolution:"
75 echo "Review the file carefully, and make sure it hasn't been "
76 echo "modified with malicious intent. When sure the "
77 echo "script is safe to execute with superuser privileges "
78 echo "you can change the scripts permissions:"
79 echo " $ sudo chmod 640 '$path'"
80 exit 1
81 fi
82 if [ "$iwgrp" != "-" ]; then # S_IWGRP
83 echo "Error: Config script '$path' cannot be writable by group!"
84 echo
85 echo "Resolution:"
86 echo "Review the file carefully, and make sure it hasn't been "
87 echo "modified with malicious intent. When sure the "
88 echo "script is safe to execute with superuser privileges "
89 echo "you can change the scripts permissions:"
90 echo " $ sudo chmod 640 '$path'"
91 exit 1
92 fi
93 }
94
95 scripts=""
96
•97
98 if test -f /etc/default/celeryd; then
99 scripts="/etc/default/celeryd"
100 _config_sanity /etc/default/celeryd
101 . /etc/default/celeryd
102 fi
103
104 EXTRA_CONFIG="/etc/default/${SCRIPT_NAME}"
105 if test -f "$EXTRA_CONFIG"; then
106 scripts="$scripts, $EXTRA_CONFIG"
107 _config_sanity "$EXTRA_CONFIG"
108 . "$EXTRA_CONFIG"
109 fi
110
111 echo "Using configuration: $scripts"
112
113 CELERY_BIN=${CELERY_BIN:-"celery"}
114 DEFAULT_USER="celery"
115 DEFAULT_PID_FILE="/var/run/celery/beat.pid"
116 DEFAULT_LOG_FILE="/var/log/celery/beat.log"
117 DEFAULT_LOG_LEVEL="INFO"
118 DEFAULT_CELERYBEAT="$CELERY_BIN"
119
120 CELERYBEAT=${CELERYBEAT:-$DEFAULT_CELERYBEAT}
121 CELERYBEAT_LOG_LEVEL=${CELERYBEAT_LOG_LEVEL:-${CELERYBEAT_LOGLEVEL:-$DEFAULT_LOG_LEVEL}}
122
123 CELERYBEAT_SU=${CELERYBEAT_SU:-"su"}
124 CELERYBEAT_SU_ARGS=${CELERYBEAT_SU_ARGS:-""}
125
•126 DEFAULT_ENABLED="false"
127
128 ENABLED=${ENABLED:-$DEFAULT_ENABLED}
129 if [ "$ENABLED" != "true" ]; then
130 echo "celerybeat daemon disabled - see $scripts."
131 exit 0
132 fi
133
134 # Sets --app argument for CELERY_BIN
135 CELERY_APP_ARG=""
136 if [ ! -z "$CELERY_APP" ]; then
137 CELERY_APP_ARG="--app=$CELERY_APP"
138 fi
139
140 CELERYBEAT_USER=${CELERYBEAT_USER:-${CELERYD_USER:-$DEFAULT_USER}}
141
142 # Set CELERY_CREATE_DIRS to always create log/pid dirs.
143 CELERY_CREATE_DIRS=${CELERY_CREATE_DIRS:-0}
144 CELERY_CREATE_RUNDIR=$CELERY_CREATE_DIRS
145 CELERY_CREATE_LOGDIR=$CELERY_CREATE_DIRS
146 if [ -z "$CELERYBEAT_PID_FILE" ]; then
147 CELERYBEAT_PID_FILE="$DEFAULT_PID_FILE"
148 CELERY_CREATE_RUNDIR=1
149 fi
150 if [ -z "$CELERYBEAT_LOG_FILE" ]; then
151 CELERYBEAT_LOG_FILE="$DEFAULT_LOG_FILE"
152 CELERY_CREATE_LOGDIR=1
153 fi
154
155 export CELERY_LOADER
156
157 if [ -n "$2" ]; then
158 CELERYBEAT_OPTS="$CELERYBEAT_OPTS $2"
159 fi
160
161 CELERYBEAT_LOG_DIR=`dirname $CELERYBEAT_LOG_FILE`
162 CELERYBEAT_PID_DIR=`dirname $CELERYBEAT_PID_FILE`
163
164 # Extra start-stop-daemon options, like user/group.
165
166 CELERYBEAT_CHDIR=${CELERYBEAT_CHDIR:-$CELERYD_CHDIR}
167 if [ -n "$CELERYBEAT_CHDIR" ]; then
168 DAEMON_OPTS="$DAEMON_OPTS --workdir=$CELERYBEAT_CHDIR"
169 fi
170
171
172 export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
173
174 check_dev_null() {
175 if [ ! -c /dev/null ]; then
176 echo "/dev/null is not a character device!"
177 exit 75 # EX_TEMPFAIL
178 fi
179 }
180
181 maybe_die() {
182 if [ $? -ne 0 ]; then
183 echo "Exiting: $*"
184 exit 77 # EX_NOPERM
185 fi
186 }
187
188 create_default_dir() {
189 if [ ! -d "$1" ]; then
190 echo "- Creating default directory: '$1'"
191 mkdir -p "$1"
192 maybe_die "Couldn't create directory $1"
193 echo "- Changing permissions of '$1' to 02755"
194 chmod 02755 "$1"
195 maybe_die "Couldn't change permissions for $1"
196 if [ -n "$CELERYBEAT_USER" ]; then
197 echo "- Changing owner of '$1' to '$CELERYBEAT_USER'"
198 chown "$CELERYBEAT_USER" "$1"
199 maybe_die "Couldn't change owner of $1"
200 fi
201 if [ -n "$CELERYBEAT_GROUP" ]; then
202 echo "- Changing group of '$1' to '$CELERYBEAT_GROUP'"
203 chgrp "$CELERYBEAT_GROUP" "$1"
204 maybe_die "Couldn't change group of $1"
205 fi
206 fi
207 }
208
209 check_paths() {
210 if [ $CELERY_CREATE_LOGDIR -eq 1 ]; then
211 create_default_dir "$CELERYBEAT_LOG_DIR"
212 fi
213 if [ $CELERY_CREATE_RUNDIR -eq 1 ]; then
214 create_default_dir "$CELERYBEAT_PID_DIR"
215 fi
216 }
217
218
219 create_paths () {
220 create_default_dir "$CELERYBEAT_LOG_DIR"
221 create_default_dir "$CELERYBEAT_PID_DIR"
222 }
223
224 is_running() {
225 pid=$1
226 ps $pid > /dev/null 2>&1
227 }
228
229 wait_pid () {
230 pid=$1
231 forever=1
232 i=0
233 while [ $forever -gt 0 ]; do
234 if ! is_running $pid; then
235 echo "OK"
236 forever=0
237 else
238 kill -TERM "$pid"
239 i=$((i + 1))
240 if [ $i -gt 60 ]; then
241 echo "ERROR"
242 echo "Timed out while stopping (30s)"
243 forever=0
244 else
245 sleep 0.5
246 fi
247 fi
248 done
249 }
250
251
252 stop_beat () {
253 echo -n "Stopping ${SCRIPT_NAME}... "
254 if [ -f "$CELERYBEAT_PID_FILE" ]; then
255 wait_pid $(cat "$CELERYBEAT_PID_FILE")
256 else
257 echo "NOT RUNNING"
258 fi
259 }
260
261 _chuid () {
262 ${CELERYBEAT_SU} ${CELERYBEAT_SU_ARGS} \
•263 "$CELERYBEAT_USER" --shell=/bin/sh -c "$CELERYBEAT $*"
264 }
265
266 start_beat () {
267 echo "Starting ${SCRIPT_NAME}..."
268 _chuid $CELERY_APP_ARG $DAEMON_OPTS beat --detach \
269 --pidfile="$CELERYBEAT_PID_FILE" \
270 --logfile="$CELERYBEAT_LOG_FILE" \
271 --loglevel="$CELERYBEAT_LOG_LEVEL" \
272 $CELERYBEAT_OPTS
273 }
274
275
276 check_status () {
277 local failed=
278 local pid_file=$CELERYBEAT_PID_FILE
279 if [ ! -e $pid_file ]; then
280 echo "${SCRIPT_NAME} is down: no pid file found"
281 failed=true
282 elif [ ! -r $pid_file ]; then
283 echo "${SCRIPT_NAME} is in unknown state, user cannot read pid file."
284 failed=true
285 else
286 local pid=`cat "$pid_file"`
287 local cleaned_pid=`echo "$pid" | sed -e 's/[^0-9]//g'`
288 if [ -z "$pid" ] || [ "$cleaned_pid" != "$pid" ]; then
289 echo "${SCRIPT_NAME}: bad pid file ($pid_file)"
290 failed=true
291 else
292 local failed=
293 kill -0 $pid 2> /dev/null || failed=true
294 if [ "$failed" ]; then
295 echo "${SCRIPT_NAME} (pid $pid) is down, but pid file exists!"
296 failed=true
297 else
298 echo "${SCRIPT_NAME} (pid $pid) is up..."
299 fi
300 fi
301 fi
302
303 [ "$failed" ] && exit 1 || exit 0
304 }
305
306
307 case "$1" in
308 start)
309 check_dev_null
310 check_paths
311 start_beat
312 ;;
313 stop)
314 check_paths
315 stop_beat
316 ;;
317 reload|force-reload)
318 echo "Use start+stop"
319 ;;
320 status)
321 check_status
322 ;;
323 restart)
324 echo "Restarting celery periodic task scheduler"
325 check_paths
326 stop_beat && check_dev_null && start_beat
327 ;;
328 create-paths)
329 check_dev_null
330 create_paths
331 ;;
332 check-paths)
333 check_dev_null
334 check_paths
335 ;;
336 *)
337 echo "Usage: /etc/init.d/${SCRIPT_NAME} {start|stop|restart|create-paths|status}"
338 exit 64 # EX_USAGE
339 ;;
340 esac
341
342 exit 0