Need help running Python app as service in Ubuntu with Upstart
Asked Answered
G

1

13

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.

Garrek answered 14/4, 2010 at 21:21 Comment(3)
When you try sudo start greeenlog, does the error message still say (uid=1000 pid=2496 comm="start)? By the way, I set up a /etc/init/greeenlog.conf as you described, and was successful with the command sudo start greeenlog.Polka
Good call! sudo start greeenlog returns greeenlog start/running, process 2609, but the application doesn't really start and nothing is logged. A subsequent call to status greeenlog returns greeenlog stop/waiting. Again, the app runs perfectly with sudo /usr/local/greeenlog/main.pyw. What am I missing?Garrek
It sounds like main.pyw might be crashing when run through upstart. Perhaps try putting exec /usr/local/greeenlog/main.pyw >/tmp/main.out 2>&1 in greeenlog.conf so you can see some error messages.Polka
G
12

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!

Garrek answered 15/4, 2010 at 15:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.