Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I've put together an upstart script for memcached based on the init.d script it ships with, as I couldn't find any examples anywhere. The problem is it's not respawning automatically when I kill the process.

env DAEMON=/usr/bin/memcached
env DAEMONBOOTSTRAP=/usr/share/memcached/scripts/start-memcached

start on started
stop on runlevel [!2345]

respawn

post-stop script
  start-stop-daemon --stop --pidfile /var/run/memcached.pid --name memcached --chuid nobody --user nobody --exec $DAEMON --signal TERM
end script

exec start-stop-daemon --start --quiet --exec $DAEMONBOOTSTRAP
share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

This is a good start, but there are a few things you may have misunderstood about upstart here:

start on started

The started event is emitted every time any job on the system is started. You probably meant to have something else after started, like start on started networking. That would be incorrect too unfortunately, as networking is not really as meaningful as its name would imply. For memcached, it can pretty much run any time after runlevel 2 has been reached. So

start on runlevel [2345]

Works, and is necessary given your stop on rule:

stop on runlevel [!2345]

I know its a bit confusing, but you actually have to use '^' instead of '!' here, so you want

stop on runlevel [^2345]

Also its worth noting that this will stop on runlevel 1, which is "single user maintenance mode". But your original start on would not start back up on runlevel 2. That would be a bug, so make sure that runlevels are respected properly.

Your post-stop and exec ignore the fact that upstart is going to try and track this pid, but because the start-memcached script exits (because it lets memcached daemonize itself) the pid will be lost. This means upstart can't respawn, because it is not aware of the pid in the first place, and doesn't know that it has died.

If you want to be able to have it respawned, you probably want:

expect daemon
exec $DAEMONBOOTSTRAP

There is no need to use start-stop-daemon in this instance. Upstart will keep track of the pid and when you do 'stop memcached' it will send it a SIGTERM. Also the config file of memcached already runs memcached as a user other than root (memcache actually) in Ubuntu 10.10 and later, so you probably don't need to worry about changing userid either.

share|improve this answer
add comment

Here is the Upstart script I've been using for memcached. This is strongly influenced by SpamapS response, but with a key tweak to drop use of start-memcached.

Using start-memcached results in three process forks to create the final memcached daemon process. Upstart appears to only supports zero through three forks:

  • Zero forks with no expect clause,
  • One forks with the expect fork clause, or
  • Two forks with with the expect daemon clause.

In my testing using expect daemon with the start-memcached wrapper occasionally resulted in Upstart tracking the wrong process ID, at which point Upstart enters a broken state where Upstart hangs when trying to start or stop the process. Bummer situation that is difficult to fix without a reboot. The discussion leading to comment number 47, here, has some good advice on whatto do if this occurs.

As a result I've moved away from using the start-memcached wrapper and simply inlined memcached configuration within the Upstart script below. Since the process is run directly by Upstart there is no need for an expect clause. A few other notes are included in-line.

description "memcached"

env MEMCACHED=/usr/bin/memcached

start on runlevel [2345]
# Not sure why it was recommended to use ^ rather than !.  I'm sticking with !.
stop on runlevel [!2345]

# This test is completely optional, I'm just paranoid.
pre-start script
  test -x $MEMCACHED || { stop; exit 0; }
end script

respawn
exec $MEMCACHED -m 384 -p 11211 -u memcache -l 127.0.0.1
share|improve this answer
add comment

Your Answer

 
discard

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.