Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is my upstart file /etc/init/myapp.conf:

description "Example of starting Node with Upstart and Forever"

start on filesystem or runlevel [2345]
stop on runlevel [06]

expect fork

console output

setuid lwood

env HOME=/home/lwood
env LOG="/home/lwood/myapp/applog.log"    

  cd $HOME
  exec forever -a -l $LOG \
       start /home/lwood/myapp/app.js
end script

pre-stop script
  cd $HOME
  exec forever stop /home/lwood/myapp/app.js >> $LOG
end script

Do you know why sudo restart myapp is only stopping the service? It does not start the service after stopping it. So it has the same effect as sudo stop myapp.

Doing sudo stop myapp and then sudo start myapp works fine though. Wondering why restarting behaves different.

I'm on Ubuntu 12.04.

share|improve this question

There is a bug in upstart. It still exists in Ubuntu Raring 13.04, but I haven't tested Saucy or later yet.

Basically, pre-stop should be avoided. It causes undefined state changes on restart. If you're using it to trigger the shutdown of a daemon, as in your case, it will just stop everything. In a more classical usage, even specifying a pre-stop script will prevent your main process from restarting (as demonstrated in the bug report).

The behavior of "on stopping" jobs seems to be unaffected, so one solution is to create a myapp-stop.conf job that is triggered "on stopping myapp". The only apparent disadvantage is that it's more difficult to know when all other potentially "on stopping myapp" jobs are completed.

share|improve this answer

To expand on xdissent's answer (which worked for me), here's an example script you can place in /etc/init/tomcat-stop.conf. When the tomcat service gets stopped, this script runs and cleanly shuts down Tomcat before Upstart restarts it.

description "Tomcat Server shutdown script"

setuid tomcat_user
setgid tomcat_group

chdir /usr/local/tomcat

start on stopping tomcat

kill timeout 30
    exec bin/ stop
end script
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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