celeryd.init 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #!/bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides: celeryd
  4. # Required-Start: $network $local_fs $remote_fs
  5. # Required-Stop: $network $local_fs $remote_fs
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop: 0 1 6
  8. # Short-Description: celery task worker daemon
  9. ### END INIT INFO
  10. #
  11. # ============================================
  12. # celeryd - Starts the Celery worker daemon.
  13. # ============================================
  14. #
  15. # :Usage: /etc/init.d/${basename $0} {start|stop|restart|status}
  16. # :Configuration file: /etc/default/celeryd
  17. #
  18. # To implement separate init scripts, do NOT copy this script. Instead,
  19. # symlink it. I.e., if my new application, "little-worker" needs an init, I
  20. # should just use:
  21. #
  22. # ln -s /etc/init.d/celeryd /etc/init.d/little-worker
  23. #
  24. # You can then configure this by manipulating /etc/sysconfig/little-worker. It
  25. # will still read defaults from /etc/defaults/celeryd, but everything can be
  26. # overriden by sysconfig.
  27. #
  28. # Setting `prog` here allows you to symlink this init script, making it easy
  29. # to run multiple processes on the system.
  30. prog="$(basename $0)"
  31. # Source the centos stuff
  32. . /etc/init.d/functions
  33. # Also look at sysconfig; this is where environmental variables should be set
  34. # on RHEL systems.
  35. [ -f "/etc/sysconfig/$prog" ] && . /etc/sysconfig/$prog
  36. CELERYD=${CELERYD:-"-m celery.bin.celeryd_detach"}
  37. CELERYD_MULTI=${CELERYD_MULTI:-"/usr/bin/celeryd-multi"}
  38. CELERYD_PID_FILE=${CELERYD_PID_FILE:-"/var/run/celeryd/$prog.pid"}
  39. CELERYD_LOG_FILE=${CELERYD_LOG_FILE:-"/var/log/celeryd/$prog.log"}
  40. CELERYD_LOG_LEVEL=${CELERYD_LOG_LEVEL:-"INFO"}
  41. # This is used to change how Celery loads in the configs. It does not need to
  42. # be set to be run.
  43. export CELERY_LOADER
  44. start_workers () {
  45. CELERYD_LOG_DIR=$(dirname $CELERYD_LOG_FILE)
  46. CELERYD_PID_DIR=$(dirname $CELERYD_PID_FILE)
  47. # Ensure that the directories exist.
  48. mkdir -p $CELERYD_LOG_DIR $CELERYD_PID_DIR
  49. # If we specified a user, and/or a group, chown as needed
  50. if [ -n "$CELERYD_USER" ]; then
  51. CHOWN_UG="${CELERYD_USER}"
  52. # If the group is specified, also use that in the chown.
  53. [ -n "$CELERYD_GROUP" ] && CHOWN_UG="$CHOWN_UG:$CELERYD_GROUP"
  54. # Execute the chown on the directory only
  55. chown $CHOWN_UG $CELERYD_LOG_DIR $CELERYD_PID_DIR
  56. CELERYD_OPTS="$CELERYD_OPTS --uid=$CELERYD_USER"
  57. fi
  58. # If we need to be run from a specific location, cd to it before launch
  59. if [ -n "$CELERYD_CHDIR" ]; then
  60. cd $CELERYD_CHDIR
  61. fi
  62. echo -n $"Starting $prog: "
  63. $CELERYD_MULTI start $prog \
  64. --pidfile=$CELERYD_PID_FILE \
  65. --logfile=$CELERYD_LOG_FILE \
  66. --loglevel=$CELERYD_LOG_LEVEL \
  67. --cmd="$CELERYD" \
  68. --quiet \
  69. $CELERYD_OPTS
  70. RETVAL=$?
  71. if [ "$RETVAL" == "0" ]; then
  72. touch /var/lock/subsys/$prog
  73. success
  74. else
  75. failure
  76. fi
  77. echo
  78. }
  79. stop_workers () {
  80. echo -n $"Stopping $prog: "
  81. # If we haven't ended, explicitly kill it!
  82. if [ ! -f $CELERYD_PID_FILE ] || [ ! -e /proc/$(cat $CELERYD_PID_FILE) ]; then
  83. failure
  84. echo
  85. return
  86. fi
  87. # First, try to nicely shut it down.
  88. $CELERYD_MULTI stop $prog --pidfile=$CELERYD_PID_FILE --quiet
  89. RETVAL=$?
  90. # SLeep a few seconds. (this was part of the original script; we can't
  91. # trust that it will end immediately, or that running the command will
  92. # stop it.
  93. sleep 3
  94. # If we haven't ended, explicitly kill it!
  95. if [ -f $CELERYD_PID_FILE ] && [ -e /proc/$(cat $CELERYD_PID_FILE) ]; then
  96. $CELERYD_MULTI stop $prog -KILL --pidfile=$CELERYD_PID_FILE --quiet
  97. fi
  98. if [ "$RETVAL" == "0" ]; then
  99. rm -f /var/lock/sybsys/$prog
  100. success
  101. else
  102. failure
  103. fi
  104. echo
  105. }
  106. case "$1" in
  107. start)
  108. start_workers ;;
  109. stop)
  110. stop_workers ;;
  111. status)
  112. status -p $CELERYD_PID_FILE $prog ;;
  113. restart)
  114. stop_workers
  115. start_workers ;;
  116. *)
  117. echo "Usage: /etc/init.d/$prog {start|stop|restart|status}"
  118. exit 1
  119. ;;
  120. esac
  121. exit 0