Sunday, May 5, 2024
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 1881  / 3 Years ago, tue, may 4, 2021, 8:18:00

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

More From » upstart

 Answers
5

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.


[#40582] Thursday, May 6, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
atchcommo

Total Points: 114
Total Questions: 130
Total Answers: 101

Location: Cook Islands
Member since Sat, Oct 16, 2021
3 Years ago
;