需要帮助在Ubuntu中使用Upstart将Python应用程序作为服务运行

时间:2010-04-14 21:21:37

标签: python ubuntu upstart

我已经在Python中编写了一个日志应用程序,它应该在启动时启动,但我无法使用Ubuntu's Upstart init daemon启动应用程序。当使用 sudo /usr/local/greeenlog/main.pyw 从终端运行时,应用程序可以正常运行。以下是我为Upstart工作所尝试的内容:

/etc/init/greeenlog.conf

# greeenlog

description     "I log stuff."

start on startup
stop on shutdown

script
    exec /usr/local/greeenlog/main.pyw
end script

我的应用程序启动一个子线程,以防重要。我已经尝试了 expect fork 节,但结果没有任何变化。我也尝试使用 sudo 并且没有脚本语句(只是一个单独的exec语句)。在所有情况下,启动后,运行状态greeenlog 会返回 greeenlog停止/等待并运行启动greeenlog 会返回:

start: Rejected send message, 1 matched rules; type="method_call", sender=":1.61" (uid=1000 pid=2496 comm="start) interface="com.ubuntu.Upstart0_6.Job" member="Start" error name="(unset)" requested_reply=0 destination="com.ubuntu.Upstart" (uid=0 pid=1 comm="/sbin/init"))

谁能看到我做错了什么?我感谢您提供的任何帮助。感谢。

1 个答案:

答案 0 :(得分:12)

感谢unutbu的帮助,我能够纠正我的工作。显然,这些是Upstart设置的唯一环境变量(使用 os.environ 在Python中检索):

{'TERM': 'linux', 'PWD': '/', 'UPSTART_INSTANCE': '', 'UPSTART_JOB': 'greeenlog', 'PATH': '/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin'}

我的程序依赖于设置的几个变量,因此这里是具有正确环境变量的修订作业:

# greeenlog

description     "I log stuff."

start on startup
stop on shutdown

env DISPLAY=:0.0
env GTK_RC_FILES=/etc/gtk/gtkrc:/home/greeenguru/.gtkrc-1.2-gnome2

script
    exec /usr/local/greeenlog/main.pyw > /tmp/greeenlog.out 2>&1
end script

谢谢!