Search code examples
ubuntucouchdbupstart

How to get custom couchdb to run as upstart job?


I built a recent version of CouchDB with the excellent iriscouch/build-couchdb repo, and now I want to create an upstart job on my Ubuntu 12.04 server.

However, the upstart job fails with:

kernel: [265024.963594] init: couchdb_a main process (5341) terminated with status 1

The upstart conf file (/etc/init/couchdb_a.conf):

description     "CouchDB v1.2.0"
console output

start on (local-filesystems and net-device-up IFACE!=lo)
stop on runlevel [!2345]

pre-start script
  logger -t "$0" "pre-start"
  export PATH="/path/to/build-couchdb/build/bin:$PATH"
  logger -t "$0" "DEBUG (pre-start): `set`"
end script

exec /path/to/build-couchdb/build/bin/couchdb 

From what I can tell, the custom couchdb simply needs its PATH adjusted to the location of the couchdb binary (that's what iriscouch's env.sh does).

But whether I update the PATH or not, the custom couchdb will not launch in this upstart job (it works perfectly from a shell).


Solution

  • This upstart configuration successfully handles a custom install of CouchDB:

    # couchdb v1.2.0
    #
    # Custom installation of CouchDB
    
    description     "CouchDB v1.2.0, local"
    console output
    
    # start after all filesystems & network interfae are available
    start on (local-filesystems and net-device-up IFACE!=lo)
    stop on runlevel [!2345]
    
    # set working directory
    env COUCHDB_WD="/path/to/build-couchdb/build/bin"
    export COUCHDB_WD
    
    # required for erlang
    env HOME="/home/user"
    export HOME
    
    script
      # modify PATH to hit local couchdb's working directory first
      PATH="$COUCHDB_WD:$PATH"
      #export PATH # not necessary inside script block
      #logger -t $0 "HOME='$HOME'"
      #logger -t $0 "PATH='$PATH'"
      # output couchdb logs to custom location
      #exec >>/home/user/couchdb_local.log 2>&1
      exec couchdb
    end script
    

    This helped, but only partially:

    # change working directory: not effective with env variable
    chdir /path/to/build-couchdb/build/bin
    

    chdir works if just the executable needs to be run from a directory, but doesn't modify PATH, and does not accept env variables.

    The commented lines in the script block helped me ensure that HOME and PATH were properly updated. The exec >> line outputs all couchdb logging to a custom file, which helped me find the root cause: Erlang.

    Specifically, the erlexec: HOME must be set error. HOME can be apparently any value (not sure if it must exist, but I set it to my user account's home directory).