Need help running Python app as service in Ubuntu with Upstart

江枫思渺然 提交于 2019-12-20 10:34:33

问题


I have written a logging application in Python that is meant to start at boot, but I've been unable to start the app with Ubuntu's Upstart init daemon. When run from the terminal with sudo /usr/local/greeenlog/main.pyw, the application works perfectly. Here is what I've tried for the Upstart job:

/etc/init/greeenlog.conf

# greeenlog

description     "I log stuff."

start on startup
stop on shutdown

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

My application starts one child thread, in case that is important. I've tried the job with the expect fork stanza without any change in the results. I've also tried this with sudo and without the script statements (just a lone exec statement). In all cases, after boot, running status greeenlog returns greeenlog stop/waiting and running start greeenlog returns:

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"))

Can anyone see what I'm doing wrong? I appreciate any help you can give. Thanks.


回答1:


Thanks to unutbu's help, I have been able to correct my job. Apparently, these are the only environment variables that Upstart sets (retrieved in Python with os.environ):

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

My program relies on a couple of these variables being set, so here is the revised job with the right environment variables:

# 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

Thank you!



来源:https://stackoverflow.com/questions/2641136/need-help-running-python-app-as-service-in-ubuntu-with-upstart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!