9

In an upstart configuration how do I define what command should be used to shutdown a server. Lets say I have daemon that has two seperate processes.

  • startup.sh - to start the server
  • shutdown.sh - to shut down the server

Suppose I have an upstart file that looks like this.

description "Some Example Service"
author "Me"

start on runlevel [2345]
stop on runlevel [016]
respawn

exec startup.sh 

How do I tell upstart that it should call shutdown.sh when shutting down the server? How does upstart normally expect to shut down a daemon?

Ethan Furman
  • 115
  • 8
ams
  • 799
  • 1
  • 6
  • 11

1 Answers1

10

Using exec to start the process is fine, but when stopping it, if you absolutely need to run a specific script or set of commands, you can use the pre-stop stanza.

pre-stop exec shutdown.sh

I assume you have tried just using an exec stanza and it didn't work, possibly because your daemon requires some specialized cleanup. Note that well-behaved daemons should receive a signal from upstart when the job is stopped, and if they handle the signal appropriately no additional cleanup should be needed.

Stopping a job involves sending SIGTERM to it. If there is anything that needs to be done before SIGTERM, do it here. Arguably, services should handle SIGTERM very gracefully, so this shouldn't be necessary.

if there is anything critical, like a flush to disk, and raising kill timeout is not an option, pre-stop is not a bad place to do it

Also read this section for the entire workflow of what happens when you stop a job. Notice when pre-stop is run, and how a signal is sent to the process afterwards, which is how upstart handles stopping jobs normally.

My source is the official Upstart cookbook at: http://upstart.ubuntu.com/cookbook/

roadmr
  • 34,222
  • 9
  • 81
  • 93
  • 1
    "Note that well-behaved daemons should receive a signal from upstart when the job is stopped, and if they handle the signal appropriately no additional cleanup should be needed." - note that we use scripts in Unix world in order to have control of things beside programers job. And this is ridiculous remark, because we need a place to include for example sending an email, removing lock from the dis etc. and this tasks do not have to be part o daemon itself. So upstart is not ready to be used into servers environment. – kakaz Dec 26 '13 at 21:32
  • From this answer ( the One!) i see that there is no good way do do what we need? – kakaz Dec 26 '13 at 21:32