[Twisted-Python] Exit code using twistd

Chris Miles miles.chris at gmail.com
Tue Apr 29 21:54:39 EDT 2008

On 29/04/2008, at 9:56 PM, Miguel Filho wrote:
> Hello list,
> I'm calling my program using twistd, like this:
> # twistd -y /usr/lib/python2.4/site-packages/apolicy/server.py -q
> --logfile /var/log/twistd.log --pidfile=/var/run/twistd.pid
> All right, no problem. But my program makes some tests while starting
> the service, like loading configuration files, etc.
> If there is any exception twistd keeps running, so I tried calling
> reactor.stop(), but I get an RuntimeError saying that it is not
> possible to stop the reactor because it is not running. So I tried
> calling sys.exit(1). twistd exits, but the code I'm passing is not
> returned by twistd, witch returns 0. This messes with my init scripts,
> so if my user calls 'service mydaemon start' the system will print an
> 'OK', but twistd will not be running in case of any errors.
> How can I proceed to avoid this behavior?

Are you using the Twisted Application Framework to initialise your app?

If I try to replicate what you describe with one my apps, forcing an  
error during configuration file parsing I get a startup failure with  
non-0 status. e.g.:

$ twistd -y vencoderd.tac -q
Failed to load application: invalid literal for int(): asdf
$ echo $?

I actually deploy my apps as a Twisted Plugin, http://twistedmatrix.com/projects/core/documentation/howto/tap.html 
, which makes life a little easier, but gives the same behaviour as  
above. e.g.

$ twistd vencoderd -c configuration.ini
$ echo $?
$ tail -2 twistd.log
2008/04/30 12:47 +1100 [-]     self.options[option_name] =  
2008/04/30 12:47 +1100 [-] ValueError: invalid literal for int(): asdf

In this case, the error ended up in the log file.

In my application, I parse the configuration file and set everything  
up before creating the service.Application and adding services.

Chris Miles

More information about the Twisted-Python mailing list