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.


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"
    [ ! -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

    #Originally this stanza didn't exist at all
    if [ -f "$DEFAULTFILE" ]; then
        . "$DEFAULTFILE"
    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
    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:

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

  • 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 1 '12 at 16:07

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

|improve this answer|||||
  • 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 8 '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 8 '12 at 21:25

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.