Search code examples
pythondjangoredisrabbitmqcelery

How to Kill all Celery background proces


Killing all celery processes involves 'grep'ing on 'ps' command and run kill command on all PID. Greping on ps command results in showing up self process info of grep command. In order to skip that PID, 'grep -v grep' command is piplined which executes 'not' condition on 'grep' search key. The 'awk' command is used to filter only 2nd and 'tr' command translates result of 'awk' command output from rows to columns. Piplining 'kill' command did not work and so the entire process has been set as command substitution, i.e., '$()'. The redirection at the end is not mandatory. Its only to supress the enitre output into background.

kill -9 $(ps aux | grep celery | grep -v grep | awk '{print $2}' | tr '\n' ' ') > /dev/null 2>&1

Solution

  • Apart from the standard unix ways of killing celery processes, Celery also provides API to kill all the workers listening on a particular broker.

    1. To kill using python. You can refer to the docs here or here. Former calls the latter function internally.

    app.control.shutdown()

    where app is the celery app instance configured with the broker.

    1. Celery command line interface can also be used for the same.

    celery -A app_name control shutdown