Ubuntu – Why does upstart keep respawning the process

servertmuxupstart

I wrote an upstart script to launch a daemon inside a tmux session. It works well and respawns the process if it dies unexpectedly, but I can't seem to stop it manually.

The job (called bukkit) looks like this:

start on filesystem
stop on runlevel [!2345]

respawn
respawn limit 5 30

chdir /home/minecraft/bukkit

expect daemon
kill timeout 30

pre-start script
    test -x /home/minecraft/bukkit/craftbukkit-0.0.1-SNAPSHOT.jar || { stop; exit 0; }
end script

pre-stop script
    tmux send -t bukkit "stop"
    tmux send -t bukkit "Enter"
    sleep 10  # Wait for server to shut down properly
end script

exec tmux new-session -d -s minecraft -n bukkit "sudo -u minecraft -- /home/minecraft/java/jre1.6.0_27/bin/java -Xincgc -Xmx1G -jar /home/minecraft/bukkit/craftbukkit-0.0.1-SNAPSHOT.jar"

When I issue a stop bukkit it freezes for ~10 seconds (the sleep timer, I guess) and prints bukkit start/running, process 2391. When I set upstart to debug, I found these relevant lines in the log:

Sep 21 19:14:59 cheftest init: bukkit goal changed from start to stop
Sep 21 19:14:59 cheftest init: bukkit main process (2499) exited normally
Sep 21 19:14:59 cheftest init: bukkit main process ended, respawning
Sep 21 19:14:59 cheftest init: bukkit goal changed from stop to respawn

Why does upstart keep respawning my process when it is supposed to stop it?

Best Answer

The difficulty here is the combination of 'respawn' with a pre-stop script that tells the process to stop. From init(5):

   respawn
         A service or task with this stanza will be automatically started
         if it should stop abnormally.  All reasons for a service stopping,
         except the stop(8) command itself, are considered abnormal.  Tasks
         may exit with a zero exit status to prevent being respawned.

The documentation is a little unclear on the point of whether exiting with a zero exit status should cause a respawn. However, fundamentally you've found an upstart bug because the main process ending when the goal is 'stop' should not result in a change to 'respawn'.

To work around this bug, you should be able to use "normal exit" to tell upstart that this is a normal way to stop the job and that it should not respawn.

  normal exit STATUS|SIGNAL...
         Additional exit statuses or even signals may be added, if the
         job process terminates with any of these it will not be considered
         to have failed and will not be respawned.

         normal exit 0 1 TERM HUP

Note that in general, it would be more robust to kill the process with a signal (specifying "kill signal N" if necessary) instead of with a pre-stop process that issues commands; but of course this is not always possible if the service doesn't support clean shutdown upon receipt of a signal.

Related Question