Debian Codegraph @

extra/generic-init.d/celerybeat

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