[Twisted-Python] Exit code using twistd
![](https://secure.gravatar.com/avatar/15454db2feb01632f8b2ebe2c97efe35.jpg?s=120&d=mm&r=g)
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? Regards, Miguel
![](https://secure.gravatar.com/avatar/3e9b0b47a98cdc664bcf58fa3ff5a758.jpg?s=120&d=mm&r=g)
On 29/04/2008, at 9:56 PM, Miguel Filho wrote:
Are you using the Twisted Application Framework to initialise your app? http://twistedmatrix.com/projects/core/documentation/howto/application.html 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 $? 1 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 $? 1 $ tail -2 twistd.log 2008/04/30 12:47 +1100 [-] self.options[option_name] = int(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. Cheers, Chris Miles
![](https://secure.gravatar.com/avatar/15454db2feb01632f8b2ebe2c97efe35.jpg?s=120&d=mm&r=g)
On Tue, Apr 29, 2008 at 10:54 PM, Chris Miles <miles.chris@gmail.com> wrote:
Are you using the Twisted Application Framework to initialise your app? http://twistedmatrix.com/projects/core/documentation/howto/application.html
Yes, I am.
In my application, I parse the configuration file and set everything up before creating the service.Application and adding services.
All right, the code was much further in the process, running inside startService and then calling sys.exit(1). I was looking inside the code, and the service creation is executed AFTER calling daemonize() I suppose, so my code was calling sys.exit(1) while the process was already forked and has returned 0 to the script. I have moved all code to be loaded before creating any service at all as you suggested and raising the proper exceptions. It's working fine, thanks for the input. Regards, Miguel
![](https://secure.gravatar.com/avatar/3e9b0b47a98cdc664bcf58fa3ff5a758.jpg?s=120&d=mm&r=g)
On 29/04/2008, at 9:56 PM, Miguel Filho wrote:
Are you using the Twisted Application Framework to initialise your app? http://twistedmatrix.com/projects/core/documentation/howto/application.html 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 $? 1 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 $? 1 $ tail -2 twistd.log 2008/04/30 12:47 +1100 [-] self.options[option_name] = int(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. Cheers, Chris Miles
![](https://secure.gravatar.com/avatar/15454db2feb01632f8b2ebe2c97efe35.jpg?s=120&d=mm&r=g)
On Tue, Apr 29, 2008 at 10:54 PM, Chris Miles <miles.chris@gmail.com> wrote:
Are you using the Twisted Application Framework to initialise your app? http://twistedmatrix.com/projects/core/documentation/howto/application.html
Yes, I am.
In my application, I parse the configuration file and set everything up before creating the service.Application and adding services.
All right, the code was much further in the process, running inside startService and then calling sys.exit(1). I was looking inside the code, and the service creation is executed AFTER calling daemonize() I suppose, so my code was calling sys.exit(1) while the process was already forked and has returned 0 to the script. I have moved all code to be loaded before creating any service at all as you suggested and raising the proper exceptions. It's working fine, thanks for the input. Regards, Miguel
participants (2)
-
Chris Miles
-
Miguel Filho