Since Ubuntu relies on upstart for some time now, I would like to use an upstart job to gracefully shutdown certain applications on system shutdown or reboot. It is essential that the system's shutdown or reboot is stalled until these applications are shut down.
The applications will be started manually on occasion, and on system shutdown should automatically be ended by a script (which I already have). Since the applications can't be ended reliably without (nearly all) other services running, ending the applications has to be done before the rest of the shutdown begins.
I think I can solve this by an upstart job which will be triggered on shutdown, but I am unsure which events I should use in which manner. So far, I have read the following (partly contradicting) statements:
- There is no general shutdown event in upstart
- Use a stanza like
start on starting shutdown
in the job definition - Use a stanza like
start on runlevel [06S]
in the job definition - Use a stanza like
start on starting runlevel [06S]
in the job definition - Use a stanza like
start on stopping runlevel [!06S]
in the job definition
From these recommendations, the following questions arise:
- Is there or is there not a general shutdown event in Ubuntu's upstart?
- What is the recommended way to implement a "shutdown hook"?
- When are the events runlevel [x] triggered; is this when having entered the runlevel or when entering the runlevel?
- Can we use something like
start on starting runlevel [x]
orstart on stopping runlevel [x]
? - What would be the best solution for my problem?
Thank you very much
Best Answer
starting
andrunlevel
are separate events, so you can't meaningfully saystarting runlevel N
.The
runlevel N
event is emitted at the start of entering the runlevel. If youstart on runlevel N
then your task runs on entry. The way to run when entry to the runlevel is completed isrun on started rc RUNLEVEL=N
.As I understand it, you need a
start on runlevel [06S]
to do what you want; it should in theory run before anything else is stopped. For finer control you might usestart on stopping apache or stopping mysql or ...
so that your task runs before any of them are allowed to be shut down.Edited to change runlevel 5 to S.