Saturday, September 30, 2017

Re: How safe is it to use sys.exit() in custom commands

I don't know of any reason why that would be unsafe. You'll find several uses of sys.exit() in Django's built in commands located in django/core/management/commands.

On Friday, September 29, 2017 at 5:13:50 PM UTC-4, phep wrote:

Executive summary : everything's in the subject line.

Boring details :

I have a Django application where I use a lot of custom commands, most being
designed to be launched mostly by cron-jobs or other bash scripts of some
sorts. In this respect, the exit code value may be of utmost importance.

Up until now, as I focused on the business logic part, when things went
wrong I just raised exceptions so that the return code was set to 1. So good
so far. The problem is the output is rather uselessly verbose then. I'm in
the process of fixing things up in order to have some more concise output on

It seems to me I once read in the doc that one should not use sys.exit() in
custom commands, but can't find exactly where it was - but I found some
allusions about this in StackOverflow comments. Yet I just sgoogumbled on
this ticket where it seems safe
to use it.

AFAIAC, I would be using sys.exit() almost exclusively at the top level
inside the handle() method, while testing sub-routines return values of
catching exceptions. Tests I've run seems show this is working as I'd
expected, but I'd like to be sure there is no hidden traps before to deploy
this code on the production server.

So what is your take on this question ?

Thanks in advance,


You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
Visit this group at
To view this discussion on the web visit
For more options, visit

No comments:

Post a Comment