0

I have a web service written in Golang that I need to fire up using Upstart.

I can run the service executable (named word) on port 5555 from within it's directory manually like so:

PORT=5555 ./word

Then curl -i http://127.0.0.1:5555/api/word returns the correct response (which is some JSON).

Now, I need to run this service via Upstart. I've written this script (called word.conf):

start on runlevel [2345]
stop on runlevel [!2345]

chdir /home/word

setgid word
setuid word

export PORT=5555
exec ./word

I run it via sudo start word

status word then tells me that the service is running and I see no errors in the Upstart error logs.

When trying to hit the service again via curl -i http://127.0.0.1:5555/api/word I get a curl: (7) couldn't connect to host error.

This seems like it has to be some sort of permissions issue, but I can't pinpoint it.

I tried removing the setgid and setuid and running with no luck. I do have a user called word.

I'm on Ubuntu 12.04.3.

Any ideas?

1

If that's your complete script, then it has a two simple problems. exec should be in a script [...] end script like this:

script
    exec ./word
end script

and the export variable should be declared first with a env stanza:

env PORT=5555
export PORT

So the whole script should looks like:

start on runlevel [2345]
stop on runlevel [!2345]

chdir /home/word

setgid word
setuid word

env PORT=5555
export PORT

script
    exec ./word
end script

I would also instead of calling ./word would call the interpreter first. If it's a bash script:

exec /usr/bin/env bash ./word

python:

exec /usr/bin/env python word

References:

  • Thanks for your insights. I made all the updates you suggested, but I'm still running into the same "couldn't connect to host" error unfortunately. – Carter Oct 9 '13 at 1:57
  • Hmm, it seems to be working now. I may have just needed to run initctl reload word after I made the edits you suggested. – Carter Oct 9 '13 at 2:41
0

You need to define your environment variable using env, and specify the interpreter to call your script:

start on runlevel [2345]
stop on runlevel [!2345]

chdir /home/word

setgid word
setuid word

env PORT=5555
export PORT

exec go run word

You may not need to 'export PORT' if this is the only process that uses it, and if the interpreter is defined in the script then you may not even need to include it in the exec.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.