0

I'm writing an upstart script for a small service I've written for my colleagues. My upstart job can start the service, but when it does it only outputs queryqueue start/running; note the lack of a pid as given for other services.

#/etc/init/queryqueue.conf

description     "Query Queue Daemon"
author          "---"

start on started mysql
stop on stopping mysql

expect fork

env DEFAULTFILE=/etc/default/queryqueue
umask 007

kill timeout 30

pre-start script
    #if [ -f "$DEFAULTFILE" ]; then
    #    . "$DEFAULTFILE"
    #fi
    [ ! -f /var/run/queryqueue.sock ] || rm -rf /var/run/queryqueue.sock
    #exec /usr/local/sbin/queryqueue -s /var/run/queryqueue.sock -d -l /tmp/upstart.log -p $PIDFILE -n $NUM_WORKERS $CLEANCACHE $FLUSHCACHE $CACHECONN
end script

script
    #Originally this stanza didn't exist at all
    if [ -f "$DEFAULTFILE" ]; then
        . "$DEFAULTFILE"
    fi
    exec /usr/local/sbin/queryqueue -s /var/run/queryqueue.sock -d -l /tmp/upstart.log -p $PIDFILE -n $NUM_WORKERS $CLEANCACHE $FLUSHCACHE $CACHECONN
end script


post-start script
    for i in `seq 1 5` ; do
        [ -S /var/run/queryqueue.sock ] && exit 0
        sleep 1
    done
    exit 1
end script

The service in question is a python script, which when run without error, forks using the code below right after checking command line options and basic environmental sanity, so I tell upstart to expect fork.

pid = os.fork()
if pid != 0:
    sys.exit(0)

The script is executable, and has a python shebang. I can send the TERM signal to the process manually, and it quits gracefully. But running stop queryqueue claims queryqueue stop/waiting but the process is still alive and well. Also, it's logs indicate it never received the kill signal. I'm guessing this is because upstart doesn't know which pid it has. I've also tried expect daemon and leaving the expect clause out entirely, but there's no change in behaviour.

How can I get upstart to determine the pid of the exec'd process

sirlark
  • 211
  • 2
  • 12
  • I've also just tried removing the forking behaviour from the service, and removing expect entirely, but then `start queryqueue` runs indefinitely, presumably waiting for the service to exit. – sirlark Nov 01 '12 at 16:07

1 Answers1

1

The expect stanza only works for the "main" process ("exec" or "script" stanza). This is documented in init(5).

jamesodhunt
  • 849
  • 5
  • 4
  • Thanks for the answer, but if I place the script command in it's own stanza, then the environment variables defined in the config file (sourced in pre-start script) aren't available to exec/script. I've edited the original post to reflect the changes I've made. – sirlark Nov 08 '12 at 21:09
  • Okay, I figured it out. You need to source the config file in the main script stanza. Again, edited the original code to reflect what eventually worked. – sirlark Nov 08 '12 at 21:25