2
# ============================================
3
# celeryd - Starts the Celery worker daemon.
4
# ============================================
6
# :Usage: /etc/init.d/celeryd {start|stop|force-reload|restart|try-restart|status}
7
# :Configuration file: /etc/default/celeryd
9
# See http://docs.celeryproject.org/en/latest/tutorials/daemonizing.html#generic-init-scripts
14
# Required-Start: $network $local_fs $remote_fs
15
# Required-Stop: $network $local_fs $remote_fs
16
# Default-Start: 2 3 4 5
18
# Short-Description: celery task worker daemon
22
# To implement separate init scripts, do NOT copy this script. Instead,
23
# symlink it. I.e., if my new application, "little-worker" needs an init, I
26
# ln -s /etc/init.d/celeryd /etc/init.d/little-worker
28
# You can then configure this by manipulating /etc/default/little-worker.
30
# If you want to have separate LSB headers in each script you can source this
31
# script instead of symlinking:
34
# source /etc/init.d/celeryd
36
# Setting `SCRIPT_NAME` here allows you to symlink/source this init script,
37
# making it easy to run multiple processes on the system.
39
. /lib/lsb/init-functions
41
SCRIPT_NAME="$(basename $0)"
43
DEFAULT_PID_FILE="/var/run/celery/${SCRIPT_NAME}/%n.pid"
44
DEFAULT_LOG_FILE="/var/log/celery/${SCRIPT_NAME}/%n.log"
45
DEFAULT_LOG_LEVEL="INFO"
46
DEFAULT_NODES="celery"
47
DEFAULT_CELERYD="-m celery worker --detach"
50
CELERY_DEFAULTS=${CELERY_DEFAULTS:-"/etc/default/${SCRIPT_NAME}"}
52
[ -r "$CELERY_DEFAULTS" ] && . "$CELERY_DEFAULTS"
54
if [ "$ENABLED" != "true" ]; then
55
echo "celery daemon disabled - see $CELERY_DEFAULTS"
59
# Sets --app argument for CELERY_BIN
61
if [ ! -z "$CELERY_APP" ]; then
62
CELERY_APP_ARG="--app=$CELERY_APP"
66
# Set CELERY_CREATE_DIRS to always create log/pid dirs.
67
CELERY_CREATE_DIRS=${CELERY_CREATE_DIRS:-0}
68
CELERY_CREATE_RUNDIR=$CELERY_CREATE_DIRS
69
CELERY_CREATE_LOGDIR=$CELERY_CREATE_DIRS
70
if [ -z "$CELERYD_PID_FILE" ]; then
71
CELERYD_PID_FILE="$DEFAULT_PID_FILE"
72
CELERY_CREATE_RUNDIR=1
74
if [ -z "$CELERYD_LOG_FILE" ]; then
75
CELERYD_LOG_FILE="$DEFAULT_LOG_FILE"
76
CELERY_CREATE_LOGDIR=1
79
CELERYD_LOG_LEVEL=${CELERYD_LOG_LEVEL:-${CELERYD_LOGLEVEL:-$DEFAULT_LOG_LEVEL}}
80
CELERY_BIN=${CELERY_BIN:-"celery"}
81
CELERYD_MULTI=${CELERYD_MULTI:-"$CELERY_BIN multi"}
82
CELERYD_NODES=${CELERYD_NODES:-$DEFAULT_NODES}
87
CELERYD_OPTS="$CELERYD_OPTS $2"
90
CELERYD_LOG_DIR=`dirname $CELERYD_LOG_FILE`
91
CELERYD_PID_DIR=`dirname $CELERYD_PID_FILE`
93
# Extra start-stop-daemon options, like user/group.
94
if [ -n "$CELERYD_USER" ]; then
95
DAEMON_OPTS="$DAEMON_OPTS --uid=$CELERYD_USER"
97
if [ -n "$CELERYD_GROUP" ]; then
98
DAEMON_OPTS="$DAEMON_OPTS --gid=$CELERYD_GROUP"
101
if [ -n "$CELERYD_CHDIR" ]; then
102
DAEMON_OPTS="$DAEMON_OPTS --workdir=$CELERYD_CHDIR"
107
if [ ! -c /dev/null ]; then
108
echo "/dev/null is not a character device!"
109
exit 75 # EX_TEMPFAIL
115
if [ $? -ne 0 ]; then
116
echo "Exiting: $* (errno $?)"
121
create_default_dir() {
122
if [ ! -d "$1" ]; then
123
echo "- Creating default directory: '$1'"
125
maybe_die "Couldn't create directory $1"
126
echo "- Changing permissions of '$1' to 02755"
128
maybe_die "Couldn't change permissions for $1"
129
if [ -n "$CELERYD_USER" ]; then
130
echo "- Changing owner of '$1' to '$CELERYD_USER'"
131
chown "$CELERYD_USER" "$1"
132
maybe_die "Couldn't change owner of $1"
134
if [ -n "$CELERYD_GROUP" ]; then
135
echo "- Changing group of '$1' to '$CELERYD_GROUP'"
136
chgrp "$CELERYD_GROUP" "$1"
137
maybe_die "Couldn't change group of $1"
144
if [ $CELERY_CREATE_LOGDIR -eq 1 ]; then
145
create_default_dir "$CELERYD_LOG_DIR"
147
if [ $CELERY_CREATE_RUNDIR -eq 1 ]; then
148
create_default_dir "$CELERYD_PID_DIR"
153
create_default_dir "$CELERYD_LOG_DIR"
154
create_default_dir "$CELERYD_PID_DIR"
157
export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
161
[ ! -d "$CELERYD_PID_DIR" ] && return
162
echo `ls -1 "$CELERYD_PID_DIR"/*.pid 2> /dev/null`
167
pid_files=`_get_pid_files`
168
[ -z "$pid_files" ] && echo "${SCRIPT_NAME} is stopped" && exit 1
170
for pid_file in $pid_files; do
171
local pid=`cat "$pid_file"`
172
local cleaned_pid=`echo "$pid" | sed -e 's/[^0-9]//g'`
173
if [ -z "$pid" ] || [ "$cleaned_pid" != "$pid" ]; then
174
echo "bad pid file ($pid_file)"
187
worker_pids=`ps h --ppid $pid -o pid`
188
[ "$worker_pids" ] && echo "$worker_pids" || one_failed=true
194
if [ ! -z "$CELERYD_ULIMIT" ]; then
195
ulimit $CELERYD_ULIMIT
197
$CELERYD_MULTI $* start $CELERYD_NODES $DAEMON_OPTS \
198
--pidfile="$CELERYD_PID_FILE" \
199
--logfile="$CELERYD_LOG_FILE" \
200
--loglevel="$CELERYD_LOG_LEVEL" \
207
(C_FAKEFORK=1 start_workers --verbose)
212
$CELERYD_MULTI stopwait $CELERYD_NODES --pidfile="$CELERYD_PID_FILE"
217
$CELERYD_MULTI restart $CELERYD_NODES $DAEMON_OPTS \
218
--pidfile="$CELERYD_PID_FILE" \
219
--logfile="$CELERYD_LOG_FILE" \
220
--loglevel="$CELERYD_LOG_LEVEL" \
227
$CELERYD_MULTI kill $CELERYD_NODES --pidfile="$CELERYD_PID_FILE"
231
restart_workers_graceful () {
233
worker_pids=`_get_worker_pids`
234
[ "$one_failed" ] && exit 1
236
for worker_pid in $worker_pids; do
238
kill -HUP $worker_pid 2> /dev/null || failed=true
239
if [ "$failed" ]; then
240
echo "${SCRIPT_NAME} worker (pid $worker_pid) could not be restarted"
243
echo "${SCRIPT_NAME} worker (pid $worker_pid) received SIGHUP"
247
[ "$one_failed" ] && exit 1 || exit 0
253
pid_files=`_get_pid_files`
254
[ -z "$pid_files" ] && echo "${SCRIPT_NAME} not running (no pidfile)" && exit 1
257
for pid_file in $pid_files; do
258
local node=`basename "$pid_file" .pid`
259
local pid=`cat "$pid_file"`
260
local cleaned_pid=`echo "$pid" | sed -e 's/[^0-9]//g'`
261
if [ -z "$pid" ] || [ "$cleaned_pid" != "$pid" ]; then
262
echo "bad pid file ($pid_file)"
265
kill -0 $pid 2> /dev/null || failed=true
266
if [ "$failed" ]; then
267
echo "${SCRIPT_NAME} (node $node) (pid $pid) is stopped, but pid file exists!"
270
echo "${SCRIPT_NAME} (node $node) (pid $pid) is running..."
275
[ "$one_failed" ] && exit 1 || exit 0
308
restart_workers_graceful
338
echo "Usage: /etc/init.d/${SCRIPT_NAME} {start|stop|restart|graceful|kill|dryrun|create-paths}"