Search code examples
pythontwistedfabric

EC2 build with Fabric requires starting Twisted server at least 3x


I'm building an EC2 server using fabric, and want to launch a Twisted server for pyapns. I've tried several different ways of launching the script - simply calling sudo(...), to creating an init.d script.

The command works out to something like this:

/usr/local/bin/twistd -r epoll -u www-data -g www-data -l /var/log/apnslistener.log --pidfile=/var/run/apnslistener.pid web --class=pyapns.server.APNSServer --port=7077 -l /var/log/pyapns.log

When using fabric, whether calling the line above directly through sudo(...) or using sudo to launch the init.d script, the process won't actually launch successfully. If I log in to the box and launch it directly, the process stays launched.

I've gotten it to work through fabric, but I have to launch the init.d script 3 times before it sticks. Logging with --spew shows no exception. If I don't run it 3 times, even the .pid file never gets created.

I'm lost as to where to start figuring out why this is happening, and the 3x hack works 100% consistently. Any ideas? Thanks!

EDIT

The output of /var/log/apnslistener.log is the following. Notice how the server is only successfully started the 3rd time.

2012-04-16 01:52:08+0000 [-] Log opened.
2012-04-16 01:52:08+0000 [-] twistd 12.0.0 (/usr/bin/python 2.7.2) starting up.
2012-04-16 01:52:08+0000 [-] reactor class: twisted.internet.epollreactor.EPollReactor.
2012-04-16 01:52:09+0000 [-] Log opened.
2012-04-16 01:52:09+0000 [-] twistd 12.0.0 (/usr/bin/python 2.7.2) starting up.
2012-04-16 01:52:09+0000 [-] reactor class: twisted.internet.epollreactor.EPollReactor.
2012-04-16 01:52:10+0000 [-] Log opened.
2012-04-16 01:52:10+0000 [-] twistd 12.0.0 (/usr/bin/python 2.7.2) starting up.
2012-04-16 01:52:10+0000 [-] reactor class: twisted.internet.epollreactor.EPollReactor.
2012-04-16 01:52:10+0000 [-] Site starting on 7077
2012-04-16 01:52:10+0000 [-] Starting factory <twisted.web.server.Site instance at 0x1c6dc20>
2012-04-16 01:52:10+0000 [-] set uid/gid 33/33
2012-04-16 01:52:12+0000 [HTTPChannel,0,127.0.0.1] APNSService __init__

Solution

  • Here's a section in the docs for your use case. It's related not to the sudo, but to the pty settings of default calls. More info in the FAQ.