[Twisted-Python] how to daemonize twisted...and detatch from shell process

Hi, I'm attempting to create an ubuntu service that when started will run in the background processing beanstalk jobs. I've taken the code from beanstalk-client twisted to act as my beanstalk client ( https://github.com/dustin/beanstalk-client-twisted/blob/master/beanstalk.py) The issue I'm having is how do I set up my service (ocapi.py) so that when the daemon (ocapi) starts it detaches from the process, which isn't' happening right now. ocapi.py (service) <--this is what should detach and I put it in /usr/sbin/ocapi.py code found at http://pastebin.com/29VnXnrm ocapi (daemon) <-- goes int /etc/init.d/ocapi code found at http://pastebin.com/0QgRfTfu stephan@oc:~$ sudo /etc/init.d/ocapi start * Starting ocapi... Removing stale pidfile /var/run/ocapi.pid Connected! ...problem is I don't get my shell prompt back when i start the service, meaning it didn't detach from the parent process...hopefully someone can shed some light on this. Thanks, Stephan

On 6 Oct, 10:30 pm, schenette@gmail.com wrote:
Use twistd, which comes with daemonization features. From the looks of your code, you're halfway there already. http://twistedmatrix.com/documents/current/core/howto/basics.html#auto1 http://twistedmatrix.com/documents/current/core/howto/application.html Jean-Paul

hey Jean-Paul, I'm using twistd - if you see the daemon code it's in there to start and manage the service. For some reason the OC Daemon pastebin linkted didn't work but this one does: OC Daemon that starts the OC Service http://pastebin.com/g0UE7An8 still know don't know what to change to get the service to run and detach from the parent process. Any help is appreciated. Stephan On Sat, Oct 6, 2012 at 6:56 PM, <exarkun@twistedmatrix.com> wrote:

On 8 Oct, 12:13 am, schenette@gmail.com wrote:
I see. However, you are not using it correctly. ;) The very last line of the code you pasted is: reactor.run() When you use twistd, you do not call `reactor.run` yourself. This is why your application isn't daemonizing. Jean-Paul

On Oct 7, 2012, at 5:13 PM, Stephan <schenette@gmail.com> wrote:
The fact that "ocapi.py" is in "/usr/sbin" suggests that it's designed to be run as a script and not as a twistd plugin. The shell script that you're citing is mostly irrelevant - that's just a wrapper to get twistd to run on your platform. And, indeed, when I look at ocapi.py, I can see it has a 'reactor.run()' at the bottom of it, which means it's trying to run itself, not be run by twistd. That's not how a file passed to '--python' is supposed to work. Are you the maintainer of ocapi.py? If not, you'll have to get them to make some changes. -glyph

I also recommend looking at systemd and Upstart, even in combination with twistd. Twisted's twistd will daemonize the process, but it won't give you named services and a clean environment. -- David Strauss | david@davidstrauss.net

On 6 Oct, 10:30 pm, schenette@gmail.com wrote:
Use twistd, which comes with daemonization features. From the looks of your code, you're halfway there already. http://twistedmatrix.com/documents/current/core/howto/basics.html#auto1 http://twistedmatrix.com/documents/current/core/howto/application.html Jean-Paul

hey Jean-Paul, I'm using twistd - if you see the daemon code it's in there to start and manage the service. For some reason the OC Daemon pastebin linkted didn't work but this one does: OC Daemon that starts the OC Service http://pastebin.com/g0UE7An8 still know don't know what to change to get the service to run and detach from the parent process. Any help is appreciated. Stephan On Sat, Oct 6, 2012 at 6:56 PM, <exarkun@twistedmatrix.com> wrote:

On 8 Oct, 12:13 am, schenette@gmail.com wrote:
I see. However, you are not using it correctly. ;) The very last line of the code you pasted is: reactor.run() When you use twistd, you do not call `reactor.run` yourself. This is why your application isn't daemonizing. Jean-Paul

On Oct 7, 2012, at 5:13 PM, Stephan <schenette@gmail.com> wrote:
The fact that "ocapi.py" is in "/usr/sbin" suggests that it's designed to be run as a script and not as a twistd plugin. The shell script that you're citing is mostly irrelevant - that's just a wrapper to get twistd to run on your platform. And, indeed, when I look at ocapi.py, I can see it has a 'reactor.run()' at the bottom of it, which means it's trying to run itself, not be run by twistd. That's not how a file passed to '--python' is supposed to work. Are you the maintainer of ocapi.py? If not, you'll have to get them to make some changes. -glyph

I also recommend looking at systemd and Upstart, even in combination with twistd. Twisted's twistd will daemonize the process, but it won't give you named services and a clean environment. -- David Strauss | david@davidstrauss.net
participants (4)
-
David Strauss
-
exarkun@twistedmatrix.com
-
Glyph
-
Stephan