2

How can I return non-zero from an upstart job if it fails to launch the first time? I suspect that respawn might be the culprit here, but perhaps not.

The reason I want respawn is because myscript.py is a serivce that can exit for two separate reasons:

  • Bad configuration. (fails at startup)
  • Unexpected exception when processing data. (Runtime failure)

So I'd very much like to keep the second behavior, without the first if at all possible.

description "MyJob"

respawn

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

script
    exec python /usr/local/bin/myscript.py
end script

Anyway, I could live with the dual behaviors if the initial restart or start commands return non-zero when the script exits within a small window.

How can I do this?

5
  • Is your script a service?
    – Braiam
    Dec 5, 2013 at 17:57
  • Does service mean something specific? My script starts and runs until killed if that's what you mean.
    – Caustic
    Dec 5, 2013 at 18:06
  • Maybe daemon rings a bell? Something that continues running in the background?
    – Braiam
    Dec 5, 2013 at 18:11
  • I want to know if the script is meant to be running in the background all the time or if it's a one time only thingy.
    – Braiam
    Dec 5, 2013 at 18:24
  • Yes of course. I thought daemon/service was implied by my question. Sorry. I'll update it.
    – Caustic
    Dec 5, 2013 at 18:43

1 Answer 1

1

I figured it out.

description "MyJob"

respawn

# retry 4 times in 3 seconds
# This should ALWAYS be true:
# 4 times * sleep .5 < 3 sec
# Otherwise it will continue respawning forever
respawn limit 4 3

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

script
    exec python /usr/local/bin/myscript.py
end script

post-start script
    # Time after starting service should unquestionably be running
    sleep .5
    if ps fax | grep '[m]yscript' ; then
        exit 0
    fi
    exit 1
end script
4
  • Wait, you want to know if your script is running? upstart knows if it's running already.
    – Braiam
    Dec 5, 2013 at 19:44
  • I want to know if it started cleanly. Doing sudo service myservice start doesn't return errors if you have the respawn option set without the post-start part. With this script, sudo service myservice start will return 1 if the script fails to start. (Really though, it's failing 4 times in 3 seconds the first time you start it).
    – Caustic
    Dec 5, 2013 at 19:56
  • Can you show the content of the script instead? The problem is within the main loop of the script (through scripts shouldn't be used for running continuously) and pre-start you can use it to do checks in the way of mysciprt --check for example.
    – Braiam
    Dec 5, 2013 at 20:08
  • "Scripts shouldn't be used for running continuously." I respectfully disagree. There's a reason it was called SysV init_scripts_ before there was upstart or systemd. However, you have an excellent point. I think I'll add something along the lines of a --check that executes the script until I get to the main loop and then exit; and call that in pre-start. Thanks!
    – Caustic
    Dec 5, 2013 at 20:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .