I should run a specific script whenever a server is shutting down, ensuring that some services are always stopped, while some other, but required services are still up.

In practice: I should run a backup script, taking a snapshot of the MySQL database, after the web server on the same host is shut down (ensuring the database is in consistent state). I should also ensure that networking is available as the backup script transfers the file to another host.

I tried to create an upstart task, with following stanza:

start on (stopped apache2 and stopping mysql and net-device-up IFACE=eth0)

However this script got never run.

What stanzas would I need to ensure that the script gets run correctly? Or is it even possible with upstart?

Update: OS I have tested is Ubuntu 11.04 (natty)

  • What operating system are you running?
    – uSlackr
    Sep 2 '11 at 20:18
  • @uSlackr: I have tested this on Ubuntu 11.04 (natty) Sep 3 '11 at 16:06

Its important to remember that upstart is an event driven system, not a dependency based system.

So using "and" can be dangerous. You're asking upstart to wait until all of these events happen to start your job.

So, the first time net-device-up IFACE=eth0 is emitted (during boot if its a server, or whenever it gets plugged in if its a laptop and managed by NetworkManager), then that will be the first condition. Then upstart will wait until apache2 is stopped. Thats not going to ever be emitted if you are using the system apache2 script, because its in /etc/init.d, and won't emit starting/started/stopping/stopped events.

What you probably want is this:

start on stopping mysql
pre-start script
  if service apache2 status ; then
     exit 0
  exec /my/backup/script
end script

Note that the task is important above. It tells upstart that this job is not "done" until the main script exits. That way mysql's stopping event will block on this job until it is done. The check on apache2 may not be right.. since I'm not sure what you want to do if apache2 is still running when mysql is stopping. In fact, the two events are not coordinated and actually happen in parallel during system shutdown, so if you want them to be coordinated you probably need another upstart job which will stop/start apache2. This would suffice

start on started mysql
stop on stopping mysql

pre-start exec /etc/init.d/apache2 start
post-stop exec /etc/init.d/apache2 stop

You'd also need to remove the normal links with update-rc.d -f apache2 remove. I'd suggest calling this something like apache2-upstart so that it doesn't confuse the service command.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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