需要帮助在Ubuntu中使用Upstart将Python应用程序作为服务运行
我写了一个用Python做的日志记录应用,目的是让它在开机时自动启动,但我一直没法通过Ubuntu的Upstart初始化守护进程来启动这个应用。当我在终端用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语句)。在所有情况下,开机后运行status greeenlog返回greeenlog stop/waiting,而运行start 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 个回答
12
感谢unutbu的帮助,我已经能够修正我的工作了。显然,这些是Upstart设置的唯一环境变量(在Python中可以通过os.environ获取):
{'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
谢谢!