如何将python程序作为守护进程运行?

Viv*_*ade 7 python macos python-2.7

我编写了以下程序来将我的程序作为守护程序运行,但它没有运行;当我从 python 调试器运行程序时,它可以工作。

我正在使用 Mac os x。

/User/Library/LaunchDaemons/com.bobbob.osx.test.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd >
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.bobbob.osx.test</string>
<key>Program</key>
<string>/Users/vivekbhintade/Desktop/test.py</string>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
Run Code Online (Sandbox Code Playgroud)

/Users/vivekbhintade/Desktop/test.py

import urllib2
from datetime import datetime
import smtplib
from smtplib import SMTPException
import threading

def checkerror():

    #my code which works fine individually, which sends mail after 5 seconds to recipients.

checkerror()
Run Code Online (Sandbox Code Playgroud)

而且我还使用以下命令从终端运行程序。

launchctl load /Library/LaunchDaemons/com.bobbob.osx.test.plist 
Run Code Online (Sandbox Code Playgroud)

这不会导致任何错误。

nic*_*ero 0

您尝试过launchctl吗?

我相信这个堆栈可以回答您的问题:

在 OS X 上在后台运行 Python