| 1 |
#!/bin/sh -e |
| 2 |
# ============================================ |
| 3 |
# celeryd - Starts the Celery worker daemon. |
| 4 |
# ============================================ |
| 5 |
# |
| 6 |
# :Usage: /etc/init.d/celeryd {start|stop|force-reload|restart|try-restart|status} |
| 7 |
# :Configuration file: /etc/default/celeryd (or /usr/local/etc/celeryd on BSD) |
| 8 |
# |
| 9 |
# See https://docs.celeryq.dev/en/latest/userguide/daemonizing.html#generic-init-scripts |
| 10 |
|
| 11 |
|
| 12 |
### BEGIN INIT INFO |
| 13 |
# Provides: celeryd |
| 14 |
# Required-Start: $network $local_fs $remote_fs |
| 15 |
# Required-Stop: $network $local_fs $remote_fs |
| 16 |
# Default-Start: 2 3 4 5 |
| 17 |
# Default-Stop: 0 1 6 |
| 18 |
# Short-Description: celery task worker daemon |
| 19 |
### END INIT INFO |
| 20 |
# |
| 21 |
# |
| 22 |
# To implement separate init-scripts, copy this script and give it a different |
| 23 |
# name. That is, if your new application named "little-worker" needs an init, |
| 24 |
# you should use: |
| 25 |
# |
| 26 |
# cp /etc/init.d/celeryd /etc/init.d/little-worker |
| 27 |
# |
| 28 |
# You can then configure this by manipulating /etc/default/little-worker. |
| 29 |
# |
| 30 |
VERSION=10.1 |
| 31 |
echo "celery init v${VERSION}." |
| 32 |
if [ $(id -u) -ne 0 ]; then |
| 33 |
echo "Error: This program can only be used by the root user." |
| 34 |
echo " Unprivileged users must use the 'celery multi' utility, " |
| 35 |
echo " or 'celery worker --detach'." |
| 36 |
exit 1 |
| 37 |
fi |
| 38 |
|
| 39 |
origin_is_runlevel_dir () { |
| 40 |
set +e |
| 41 |
dirname $0 | grep -q "/etc/rc.\.d" |
| 42 |
echo $? |
| 43 |
} |
| 44 |
|
| 45 |
# Can be a runlevel symlink (e.g., S02celeryd) |
| 46 |
if [ $(origin_is_runlevel_dir) -eq 0 ]; then |
| 47 |
SCRIPT_FILE=$(readlink "$0") |
| 48 |
else |
| 49 |
SCRIPT_FILE="$0" |
| 50 |
fi |
| 51 |
SCRIPT_NAME="$(basename "$SCRIPT_FILE")" |
| 52 |
|
| 53 |
DEFAULT_USER="celery" |
| 54 |
DEFAULT_PID_FILE="/var/run/celery/%n.pid" |
| 55 |
DEFAULT_LOG_FILE="/var/log/celery/%n%I.log" |
| 56 |
DEFAULT_LOG_LEVEL="INFO" |
| 57 |
DEFAULT_NODES="celery" |
| 58 |
DEFAULT_CELERYD="-m celery worker --detach" |
| 59 |
|
| 60 |
if [ -d "/etc/default" ]; then |
| 61 |
CELERY_CONFIG_DIR="/etc/default" |
| 62 |
else |
| 63 |
CELERY_CONFIG_DIR="/usr/local/etc" |
| 64 |
fi |
| 65 |
|
| 66 |
CELERY_DEFAULTS=${CELERY_DEFAULTS:-"$CELERY_CONFIG_DIR/${SCRIPT_NAME}"} |
| 67 |
|
| 68 |
# Make sure executable configuration script is owned by root |
| 69 |
_config_sanity() { |
| 70 |
local path="$1" |
| 71 |
local owner=$(ls -ld "$path" | awk '{print $3}') |
| 72 |
local iwgrp=$(ls -ld "$path" | cut -b 6) |
| 73 |
local iwoth=$(ls -ld "$path" | cut -b 9) |
| 74 |
|
| 75 |
if [ "$(id -u $owner)" != "0" ]; then |
| 76 |
echo "Error: Config script '$path' must be owned by root!" |
| 77 |
echo |
| 78 |
echo "Resolution:" |
| 79 |
echo "Review the file carefully, and make sure it hasn't been " |
| 80 |
echo "modified with malicious intent. When sure the " |
| 81 |
echo "script is safe to execute with superuser privileges " |
| 82 |
echo "you can change ownership of the script:" |
| 83 |
echo " $ sudo chown root '$path'" |
| 84 |
exit 1 |
| 85 |
fi |
| 86 |
|
| 87 |
if [ "$iwoth" != "-" ]; then # S_IWOTH |
| 88 |
echo "Error: Config script '$path' cannot be writable by others!" |
| 89 |
echo |
| 90 |
echo "Resolution:" |
| 91 |
echo "Review the file carefully, and make sure it hasn't been " |
| 92 |
echo "modified with malicious intent. When sure the " |
| 93 |
echo "script is safe to execute with superuser privileges " |
| 94 |
echo "you can change the scripts permissions:" |
| 95 |
echo " $ sudo chmod 640 '$path'" |
| 96 |
exit 1 |
| 97 |
fi |
| 98 |
if [ "$iwgrp" != "-" ]; then # S_IWGRP |
| 99 |
echo "Error: Config script '$path' cannot be writable by group!" |
| 100 |
echo |
| 101 |
echo "Resolution:" |
| 102 |
echo "Review the file carefully, and make sure it hasn't been " |
| 103 |
echo "modified with malicious intent. When sure the " |
| 104 |
echo "script is safe to execute with superuser privileges " |
| 105 |
echo "you can change the scripts permissions:" |
| 106 |
echo " $ sudo chmod 640 '$path'" |
| 107 |
exit 1 |
| 108 |
fi |
| 109 |
} |
| 110 |
|
| 111 |
if [ -f "$CELERY_DEFAULTS" ]; then |
| 112 |
_config_sanity "$CELERY_DEFAULTS" |
| 113 |
echo "Using config script: $CELERY_DEFAULTS" |
| 114 |
. "$CELERY_DEFAULTS" |
| 115 |
fi |
| 116 |
|
| 117 |
# Sets --app argument for CELERY_BIN |
| 118 |
CELERY_APP_ARG="" |
| 119 |
if [ ! -z "$CELERY_APP" ]; then |
| 120 |
CELERY_APP_ARG="--app=$CELERY_APP" |
| 121 |
fi |
| 122 |
|
| 123 |
# Options to su |
| 124 |
# can be used to enable login shell (CELERYD_SU_ARGS="-l"), |
| 125 |
# or even to use start-stop-daemon instead of su. |
| 126 |
CELERYD_SU=${CELERY_SU:-"su"} |
| 127 |
CELERYD_SU_ARGS=${CELERYD_SU_ARGS:-""} |
| 128 |
|
| 129 |
CELERYD_USER=${CELERYD_USER:-$DEFAULT_USER} |
| 130 |
|
| 131 |
# Set CELERY_CREATE_DIRS to always create log/pid dirs. |
| 132 |
CELERY_CREATE_DIRS=${CELERY_CREATE_DIRS:-0} |
| 133 |
CELERY_CREATE_RUNDIR=$CELERY_CREATE_DIRS |
| 134 |
CELERY_CREATE_LOGDIR=$CELERY_CREATE_DIRS |
| 135 |
if [ -z "$CELERYD_PID_FILE" ]; then |
| 136 |
CELERYD_PID_FILE="$DEFAULT_PID_FILE" |
| 137 |
CELERY_CREATE_RUNDIR=1 |
| 138 |
fi |
| 139 |
if [ -z "$CELERYD_LOG_FILE" ]; then |
| 140 |
CELERYD_LOG_FILE="$DEFAULT_LOG_FILE" |
| 141 |
CELERY_CREATE_LOGDIR=1 |
| 142 |
fi |
| 143 |
|
| 144 |
CELERYD_LOG_LEVEL=${CELERYD_LOG_LEVEL:-${CELERYD_LOGLEVEL:-$DEFAULT_LOG_LEVEL}} |
| 145 |
CELERY_BIN=${CELERY_BIN:-"celery"} |
| 146 |
CELERYD_MULTI=${CELERYD_MULTI:-"$CELERY_BIN multi"} |
| 147 |
CELERYD_NODES=${CELERYD_NODES:-$DEFAULT_NODES} |
| 148 |
|
| 149 |
export CELERY_LOADER |
| 150 |
|
| 151 |
if [ -n "$2" ]; then |
| 152 |
CELERYD_OPTS="$CELERYD_OPTS $2" |
| 153 |
fi |
| 154 |
|
| 155 |
CELERYD_LOG_DIR=`dirname $CELERYD_LOG_FILE` |
| 156 |
CELERYD_PID_DIR=`dirname $CELERYD_PID_FILE` |
| 157 |
|
| 158 |
# Extra start-stop-daemon options, like user/group. |
| 159 |
if [ -n "$CELERYD_CHDIR" ]; then |
| 160 |
DAEMON_OPTS="$DAEMON_OPTS --workdir=$CELERYD_CHDIR" |
| 161 |
fi |
| 162 |
|
| 163 |
|
| 164 |
check_dev_null() { |
| 165 |
if [ ! -c /dev/null ]; then |
| 166 |
echo "/dev/null is not a character device!" |
| 167 |
exit 75 # EX_TEMPFAIL |
| 168 |
fi |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
maybe_die() { |
| 173 |
if [ $? -ne 0 ]; then |
| 174 |
echo "Exiting: $* (errno $?)" |
| 175 |
exit 77 # EX_NOPERM |
| 176 |
fi |
| 177 |
} |
| 178 |
|
| 179 |
create_default_dir() { |
| 180 |
if [ ! -d "$1" ]; then |
| 181 |
echo "- Creating default directory: '$1'" |
| 182 |
mkdir -p "$1" |
| 183 |
maybe_die "Couldn't create directory $1" |
| 184 |
echo "- Changing permissions of '$1' to 02755" |
| 185 |
chmod 02755 "$1" |
| 186 |
maybe_die "Couldn't change permissions for $1" |
| 187 |
if [ -n "$CELERYD_USER" ]; then |
| 188 |
echo "- Changing owner of '$1' to '$CELERYD_USER'" |
| 189 |
chown "$CELERYD_USER" "$1" |
| 190 |
maybe_die "Couldn't change owner of $1" |
| 191 |
fi |
| 192 |
if [ -n "$CELERYD_GROUP" ]; then |
| 193 |
echo "- Changing group of '$1' to '$CELERYD_GROUP'" |
| 194 |
chgrp "$CELERYD_GROUP" "$1" |
| 195 |
maybe_die "Couldn't change group of $1" |
| 196 |
fi |
| 197 |
fi |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
check_paths() { |
| 202 |
if [ $CELERY_CREATE_LOGDIR -eq 1 ]; then |
| 203 |
create_default_dir "$CELERYD_LOG_DIR" |
| 204 |
fi |
| 205 |
if [ $CELERY_CREATE_RUNDIR -eq 1 ]; then |
| 206 |
create_default_dir "$CELERYD_PID_DIR" |
| 207 |
fi |
| 208 |
} |
| 209 |
|
| 210 |
create_paths() { |
| 211 |
create_default_dir "$CELERYD_LOG_DIR" |
| 212 |
create_default_dir "$CELERYD_PID_DIR" |
| 213 |
} |
| 214 |
|
| 215 |
export PATH="${PATH:+$PATH:}/usr/sbin:/sbin" |
| 216 |
|
| 217 |
|
| 218 |
_get_pidfiles () { |
| 219 |
# note: multi < 3.1.14 output to stderr, not stdout, hence the redirect. |
| 220 |
${CELERYD_MULTI} expand "${CELERYD_PID_FILE}" ${CELERYD_NODES} 2>&1 |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
_get_pids() { |
| 225 |
found_pids=0 |
| 226 |
my_exitcode=0 |
| 227 |
|
| 228 |
for pidfile in $(_get_pidfiles); do |
| 229 |
local pid=`cat "$pidfile"` |
| 230 |
local cleaned_pid=`echo "$pid" | sed -e 's/[^0-9]//g'` |
| 231 |
if [ -z "$pid" ] || [ "$cleaned_pid" != "$pid" ]; then |
| 232 |
echo "bad pid file ($pidfile)" |
| 233 |
one_failed=true |
| 234 |
my_exitcode=1 |
| 235 |
else |
| 236 |
found_pids=1 |
| 237 |
echo "$pid" |
| 238 |
fi |
| 239 |
|
| 240 |
if [ $found_pids -eq 0 ]; then |
| 241 |
echo "${SCRIPT_NAME}: All nodes down" |
| 242 |
exit $my_exitcode |
| 243 |
fi |
| 244 |
done |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
_chuid () { |
| 249 |
${CELERYD_SU} ${CELERYD_SU_ARGS} "$CELERYD_USER" -c "$CELERYD_MULTI $*" |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
start_workers () { |
| 254 |
if [ ! -z "$CELERYD_ULIMIT" ]; then |
| 255 |
ulimit $CELERYD_ULIMIT |
| 256 |
fi |
| 257 |
_chuid $* start $CELERYD_NODES $DAEMON_OPTS \ |
| 258 |
--pidfile="$CELERYD_PID_FILE" \ |
| 259 |
--logfile="$CELERYD_LOG_FILE" \ |
| 260 |
--loglevel="$CELERYD_LOG_LEVEL" \ |
| 261 |
$CELERY_APP_ARG \ |
| 262 |
$CELERYD_OPTS |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
dryrun () { |
| 267 |
(C_FAKEFORK=1 start_workers --verbose) |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
stop_workers () { |
| 272 |
_chuid stopwait $CELERYD_NODES $DAEMON_OPTS --pidfile="$CELERYD_PID_FILE" |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
restart_workers () { |
| 277 |
_chuid restart $CELERYD_NODES $DAEMON_OPTS \ |
| 278 |
--pidfile="$CELERYD_PID_FILE" \ |
| 279 |
--logfile="$CELERYD_LOG_FILE" \ |
| 280 |
--loglevel="$CELERYD_LOG_LEVEL" \ |
| 281 |
$CELERY_APP_ARG \ |
| 282 |
$CELERYD_OPTS |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
kill_workers() { |
| 287 |
_chuid kill $CELERYD_NODES $DAEMON_OPTS --pidfile="$CELERYD_PID_FILE" |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
restart_workers_graceful () { |
| 292 |
echo "WARNING: Use with caution in production" |
| 293 |
echo "The workers will attempt to restart, but they may not be able to." |
| 294 |
local worker_pids= |
| 295 |
worker_pids=`_get_pids` |
| 296 |
[ "$one_failed" ] && exit 1 |
| 297 |
|
| 298 |
for worker_pid in $worker_pids; do |
| 299 |
local failed= |
| 300 |
kill -HUP $worker_pid 2> /dev/null || failed=true |
| 301 |
if [ "$failed" ]; then |
| 302 |
echo "${SCRIPT_NAME} worker (pid $worker_pid) could not be restarted" |
| 303 |
one_failed=true |
| 304 |
else |
| 305 |
echo "${SCRIPT_NAME} worker (pid $worker_pid) received SIGHUP" |
| 306 |
fi |
| 307 |
done |
| 308 |
|
| 309 |
[ "$one_failed" ] && exit 1 || exit 0 |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
check_status () { |
| 314 |
my_exitcode=0 |
| 315 |
found_pids=0 |
| 316 |
|
| 317 |
local one_failed= |
| 318 |
for pidfile in $(_get_pidfiles); do |
| 319 |
if [ ! -r $pidfile ]; then |
| 320 |
echo "${SCRIPT_NAME} down: no pidfiles found" |
| 321 |
one_failed=true |
| 322 |
break |
| 323 |
fi |
| 324 |
|
| 325 |
local node=`basename "$pidfile" .pid` |
| 326 |
local pid=`cat "$pidfile"` |
| 327 |
local cleaned_pid=`echo "$pid" | sed -e 's/[^0-9]//g'` |
| 328 |
if [ -z "$pid" ] || [ "$cleaned_pid" != "$pid" ]; then |
| 329 |
echo "bad pid file ($pidfile)" |
| 330 |
one_failed=true |
| 331 |
else |
| 332 |
local failed= |
| 333 |
kill -0 $pid 2> /dev/null || failed=true |
| 334 |
if [ "$failed" ]; then |
| 335 |
echo "${SCRIPT_NAME} (node $node) (pid $pid) is down, but pidfile exists!" |
| 336 |
one_failed=true |
| 337 |
else |
| 338 |
echo "${SCRIPT_NAME} (node $node) (pid $pid) is up..." |
| 339 |
fi |
| 340 |
fi |
| 341 |
done |
| 342 |
|
| 343 |
[ "$one_failed" ] && exit 1 || exit 0 |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
case "$1" in |
| 348 |
start) |
| 349 |
check_dev_null |
| 350 |
check_paths |
| 351 |
start_workers |
| 352 |
;; |
| 353 |
|
| 354 |
stop) |
| 355 |
check_dev_null |
| 356 |
check_paths |
| 357 |
stop_workers |
| 358 |
;; |
| 359 |
|
| 360 |
reload|force-reload) |
| 361 |
echo "Use restart" |
| 362 |
;; |
| 363 |
|
| 364 |
status) |
| 365 |
check_status |
| 366 |
;; |
| 367 |
|
| 368 |
restart) |
| 369 |
check_dev_null |
| 370 |
check_paths |
| 371 |
restart_workers |
| 372 |
;; |
| 373 |
|
| 374 |
graceful) |
| 375 |
check_dev_null |
| 376 |
restart_workers_graceful |
| 377 |
;; |
| 378 |
|
| 379 |
kill) |
| 380 |
check_dev_null |
| 381 |
kill_workers |
| 382 |
;; |
| 383 |
|
| 384 |
dryrun) |
| 385 |
check_dev_null |
| 386 |
dryrun |
| 387 |
;; |
| 388 |
|
| 389 |
try-restart) |
| 390 |
check_dev_null |
| 391 |
check_paths |
| 392 |
restart_workers |
| 393 |
;; |
| 394 |
|
| 395 |
create-paths) |
| 396 |
check_dev_null |
| 397 |
create_paths |
| 398 |
;; |
| 399 |
|
| 400 |
check-paths) |
| 401 |
check_dev_null |
| 402 |
check_paths |
| 403 |
;; |
| 404 |
|
| 405 |
*) |
| 406 |
echo "Usage: /etc/init.d/${SCRIPT_NAME} {start|stop|restart|graceful|kill|dryrun|create-paths}" |
| 407 |
exit 64 # EX_USAGE |
| 408 |
;; |
| 409 |
esac |
| 410 |
|
| 411 |
exit 0 |